[NTLUG:Discuss] Perl pattern matching problem

Douglas Scott dcscott1 at comcast.net
Tue May 2 19:37:58 CDT 2006


On Tuesday 02 May 2006 8:04 am, Patrick R. Michaud wrote:
> On Mon, May 01, 2006 at 10:51:13PM -0500, Douglas Scott wrote:
> > I have an odd problem with some Perl code.
> >
[munch]
>
> In this last loop, note that $Line is set to "reference" each element
> of @Model directly, not a copy of it.  Thus, any changes you make to
> $Line are really changing the elements in @Model.
>
> So, the first time through the loop, all of the <stno> and <pc>
> strings in @Model are being replaced by $Order and $Count.  After
> that, the strings in @Model no longer have <stno> and <pc>
> substrings to replace for subsequent passes through the outer loop.
>
> You probably want something more like:
>
>     foreach my $Spec ( <LIST> )
>        {
>        chomp $Spec;
>        ($Order, $Count) = split /\t/, $Spec;
>        $Count = sprintf "%04d", $Count;
>        print "$Order, $Count\n";
>        foreach my $Line ( @Model )
>           {
>           $x = $Line;
>           $x =~ s/<stno>/$Order/g;
>           $x =~ s/<pc>/$Count/g;
>           print $x;
>           }
>        }
>
> Pm

That was it.  I assumed that the $Line variable was a copy, rather than a 
reference.  I also assumed that the problem was with the $Order and $Count 
variables and didn't look at $Line or @Model.  It works fine now.

Thanks

Doug



More information about the Discuss mailing list