[techtalk] Perl help needed

Alex Yan flare at serv.net
Thu Jun 22 21:43:32 EST 2000


Hi Lynn,

The error below means that there are too many asterisk characters ('*') in
the regular expression, which is probably caused by the fact that there's
a '*' character in what you're trying to match (the last line in FILE2).
If you truly want to match that, try this:

  $string2 = quotemeta($string2);
  if ($string1 =~ /\*\s*$string2|$string2/) {
    # whatever...
  }

On the other hand, "$string1 =~ /$string2/" will match even if there's an
asterisk, since you're not specifying the entire string, so the first part
of the regular expression is pretty much unnecessary.

But you should probably just suck up the information into a hash, rather
than re-read the entire file again and again (which you aren't doing
anyway, which means that, if the perl compiled, you probably would have
matched only against the first line... your "debugging problem" maybe?).
Try this:

  #!/usr/bin/perl

  # Read in the arguments.
  my ($input_file, $flags_file, $output_file) = @ARGV;
  if (! $output_file) {
      die "usage: converttags.pl infile tagfile outfile\n";
  }  

  # Suck in all the flags.
  my %flags;  
  open (FLAGS, $flags_file) || die "can't open flags file: $!";
  while (my $line = <FLAGS>) {
      chomp $line;
      $flags{$line} = 1;
  }
  close FLAGS;

  # Loop through the input file and see if there are any flags.
  open (INPUT, $input_file) || die "Can't open input file: $!";
  open (OUTPUT, ">$output_file") || die "Can't open output file: $!";
  while (my $line = <INPUT>) {

      my $okay = 0;
      for my $flag (keys %flags) {
  	$flag = quotemeta($flag);
  	if ($line =~ /$flag/) {
 	    $count ++;
  	    $okay = 1;
  	} elsif ($count > 0) {
  	    if ($line =~ /\#endif/) {
  		$count --;
  	    }
  	    $okay = 1;
  	}
      }
      print OUTPUT $line if $okay;
  }
  close OUTPUT;
  close INPUT;

Hope that helps and makes sense and all!  Let me know what you're trying
to do and I can try to make some more intelligent suggestions.


-Alex Yan
 flare at serv.net

On Thu, 22 Jun 2000, Lynn Kuhlman wrote:

> Help!
> When I try to compile Converttags.pl I get the following error:
> 
> /\*\s**#ifdef   yeah
> |*#ifdef   yeah
> /: nested *?+ in regexp at converttags.pl line 15, <FILE2> chunk 13.
> 
> 
> Here's the code:
> 
> Besides the regex problem I also think there is a looping problem as well. I
> don't have a debugger at work so I'm blind.
> 
> Thanks,
> Lynn
> 
> (Converttags.pl)
> >
> >
> >#! /opt/perl/bin
> >unless (@ARGV == 3)
> >{
> >  die "usage: converttags.pl infile tagfile outfile\n";
> >}
> >else
> >{
> >($file1, $file2, $file3) = @ARGV;
> >open (FILE1, $file1) || die "I can't open $file1: $!";
> >open (FILE2, $file2) || die "I can't open $file2: $!";
> >open (FILE3, ">$file3") || die "I can't open $file3: $!";
> >$count = 0;
> > while ($string1 = <FILE1>) {
> >  while ($string2 = <FILE2>) {
> >   if  ($string1 =~ /\*\s*\$string2|\$string2/)
> >   { $count++;
> >     print FILE3 $string1;
> >   }
> >  } #end while
> > if ( ($string1 =~ /\*\#endif|#endif/) && ($count > 0 ) )
> > { $count--;
> >    print FILE3 $string1;
> > }
> > elsif ( $count > 0 )
> > { print FILE3 $string1;
> > }
> > } #end while
> >
> >(testfile)
> >
> > #ifdef SNS *
> >junk
> >junk
> >#ifdef ken
> >#ifdef don
> >help
> >me
> >* junk *
> >sando
> >anderson
> >#ifdef sam *
> >more
> >steve
> >javascript
> >#ifdef nicholas
> >#ifdef jon
> >hi
> >there
> >
> >(testflags)
> >
> >#ifdef junk
> >#ifdef hi
> >#ifdef yes
> >#ifdef ken
> >#ifdef don
> >#ifdef sure
> >#ifdef my
> >#ifdef hello
> >#ifdef bye
> >#ifdef beatles
> >#ifdef megan
> >#ifdef nicholas
> >*#ifdef   yeah
> >
> >
> >
> >
> 
> 
> 
> 
> 
> _______________________________________________
> techtalk mailing list
> techtalk at linuxchix.org
> http://www.linux.org.uk/mailman/listinfo/techtalk
> 







More information about the Techtalk mailing list