Perl help needed

Ryan Hennig rhennig at cs.washington.edu
Tue Jun 27 20:11:10 PDT 2000


My comments are in your code below.

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/)

	This currently matches:
	"*    $string2" or "$string2"
	To interpolate variables, don't escape the $ sign.  Perl
	automatically figures out that it's not an end-of-line character.

> >   { $count++;
> >     print FILE3 $string1;
> >   }
> >  } #end while
> > if ( ($string1 =~ /\*\#endif|#endif/) && ($count > 0 ) )

	don't escape the #.

> > { $count--;
> >    print FILE3 $string1;
> > }
> > elsif ( $count > 0 )
> > { print FILE3 $string1;
> > }
> > } #end while


Another tip... I realize that this is only a simple test script, but
instead of looping through the second file for every line in the first
file, it would be more efficient to store the values in the second file
into a hash, and then you can search the hash for each line in the first
file.  Of course, if you are writing this for a quick-and-dirty job, it
may not be worth the complexity.

Hope that helps,

Ryan Hennig
ACM Student Webmaster
UW Computer Science and Engineering



More information about the Linux mailing list