[NTLUG:Discuss] Perl pattern matching problem

Douglas Scott dcscott1 at comcast.net
Mon May 1 22:51:13 CDT 2006


I have an odd problem with some Perl code.

I have two small text files.  One is a list store numbers and quantities, 
separated by tabs.  The other contains 2 lines of text that contains patterns 
that I wish to replace with the numbers from the other file.  I have the 
small model file loaded into the array @Model.  

The first time through the loop the script works as expected.  But after that 
it keeps the original values for $Order and $Count and keeps using them, no 
matter what is read from the file. 

my ($Order, $Count); 

foreach my $Spec ( <LIST> ) 
   { 
   chomp $Spec; 
   ($Order, $Count) = split /\t/, $Spec; 
   $Count = sprintf "%04d", $Count; 
   print "$Order, $Count\n"; 
   foreach my $Line ( @Model ) 
      { 
      $Line =~ s/<stno>/$Order/g; 
      $Line =~ s/<pc>/$Count/g; 
      print $Line; 
      } 
   } 

My first thought is that it compiled the regular expression on the the first 
pass and is using that, ignoring any changes in values in the variables.  But 
looking through several Perl books I have found that the /o option  will make 
it behave like that, but there isn't anything that should make it do that 
right now.  It also occurred to me that there was some odd option turned on 
at work.  But I took the code home and tried it on my Linux system and I am 
seeing the same results.

Other things I have tried include putting the statements in eval, and trying 
all the options for s// I could find, whether they sounded appropriate or 
not.  No dice.

Do any of the Perl gurus out there have any ideas about this one.  I'm 
stumped.

Doug




More information about the Discuss mailing list