[NTLUG:Discuss] Perl pattern matching problem

Patrick R. Michaud pmichaud at pobox.com
Tue May 2 08:07:05 CDT 2006


On Mon, May 01, 2006 at 11:01:36PM -0500, Victor Brilon wrote:
> 
> On May 1, 2006, at 10:51 PM, Douglas Scott wrote:
> 
> > I have an odd problem with some Perl code.
> >
> [ snip ]
> 
> > 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;
> >       }
> >    }
> 
> 2 observations:
> 
> 1) Where is the @Model array defined?
> 
> 2) Since you never reinitialize $Order and $Count inside the loop, if  
> the split() returns nothing, you're picking up the values from the  
> last iteration through the loop.

Split() always returns *something*.   If $Spec doesn't contain a tab,
then $Order will have all of $Spec and $Count will be undef.
If $Spec contains a tab, then $Order will have everything up to
the first tab, and $Count will have everything after the first tab
up to the second one (if any).

(If split didn't return anything, then $Order and $Count would
be undef.)

Pm



More information about the Discuss mailing list