Thanks for the range operator idea.


-----Original Message-----
From:   King, Jason [mailto:[EMAIL PROTECTED]]
Sent:   Monday, April 23, 2001 4:29 AM
To:     [EMAIL PROTECTED]
Cc:     [EMAIL PROTECTED]
Subject:        RE: Another regular expression question?


Amarnath's code sample below can be improved in a number of ways

firstly - to read the entire file into a scalar it's easier and quicker to
do the following

  open FILE, 'Cfile' or die "Bad open: $!";
  my $program;

  {
    local $/;    # undefine the builtin var $/

    $program = <FILE>;   # slurp the whole file in
  }

secondly .. it will not behave as required if there exists the following in
code

  printf '/* %s */', blah;

and it will behave even worse if the following is in code

  printf '/* %s', blah;

  // more code

  /* some other comment */


however .. even with those items improved it has obvious memory limitations
.. which although probably not significant for a source code file - might be
significant in other places

so .. you might not be able to slurp the whole file in to a scalar to do the
substitution

let me introduce the range operators ('..' and '...') .. look them up in
perlop .. I'll just give you a working code sample


  #!perl -w
  use strict;

  open FILE, 'Cfile' or die "Bad open: $!";

  while(<FILE>)
  {
    next if m#^/\*# .. m#^\*/#;  # using the range operator

    print;     # we're just printing for testing
  }

  __END__

note: we're anchoring the comment tags to the beginning of the line .. this
could break if the comment tags are not at the beginning (although in the
code sample Yvonne provided and in most C code - they are)

--
  jason king

  In Spearfish, South Dakota, if three or more Indians are walking down
  the street together, they can be considered a war party and fired
  upon. - http://dumblaws.com/


>-----Original Message-----
>From: Amarnath Honnavalli Anantharamaiah
>[mailto:[EMAIL PROTECTED]]
>Sent: Fri 20 Apr 2001 19:54
>To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
>Subject: RE: Another regular expression question?
>
>
>I don't know how this works, But I have seen this regexp
>comparison it in
>perlop man pages. It has been very good regexp.
>Can anyone explain this for me.
>
>#! /usr/bin/perl
>
># open the file
>open(fileHandle, "Cfile") || die "can't open the file ";
>
># read the file in scalar
>while(<fileHandle>) {
> $program .= $_;
>}
>
># Delete (most) C comments.
>   $program =~ s {
>   /\*     # Match the opening delimiter.
>   .*?     # Match a minimal number of characters.
>   \*/     # Match the closing delimiter.
>} []gsx;
>
>print $program
>
>
>
>
>-----Original Message-----
>From:  [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
>Sent:  Friday, April 20, 2001 1:33 PM
>To:    [EMAIL PROTECTED]
>Subject:       Another regular expression question?
>
>I am also very new to Perl! I need to figure out how I could skip a
>block of comments in a C header file. For example, if I have something
>like the following:
>
>/* This is my block of comments.....blah
>....blah.......................................................
>.............
>.and
>lots more comments here........
>and then even more here!.................... with my end of comments
>indentifier on the next line!.........
>
>*/
>
>And then the actual code down further needs to be processed. Any help
>would mean alot to a beginner like me.
>Thanks
>YM
>
>

Reply via email to