Gunnar, thank you so much!

----- Original Message ----- 
From: "Gunnar Hjalmarsson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, October 21, 2004 4:40 PM
Subject: Re: previous element


> Vladimir Lemberg wrote:
> > I need to access previous element in array.
>
> I don't see any array. I see an extract from a file with records
> separated by blank lines.
>
> <snip>
>
> > Pattern is PARKSIDE AVE. When I find it, I wont to display the
> > previous line as well, because its one record.
> >
> > while(<TF>){
> > if (!/$pattern/i){next;}
> > else{$counter++;
> > print $_[-1]; #here I'm trying to display that previous line without
> > success.
> > print "$counter: $_";}
> > }
>
> Always
>
>      use strict;
>      use warnings;
>
> !!
>
> Also, please indent the code properly.
>
> The natural solution is to set the input record separator:
>
>      local $/ = '';
>      while (<TF>) {
>          print if /$pattern/i;
>      }
>
> Read about $/ in "perldoc perlvar".
>
> > File example:
> >
> > #REDIRECT=/net/542/2003q4_2m/db/uca/zone.cfg
> > REDIRECT=/net/542/2003q4_2m/db/uma/zone.cfg
> >
> > I need to get this path: /net/542/2003q4_2m/db/uca/
> >
> > while (<ZONECFG>){
> >   next if $_ =~ /^#/;
> >   ($path_todb) = $_ =~ /\/.*\//;
> > }
> >
> > Instead of correct path I've got - 1. It looks like I'm assigning
> > scalar value of $_ to my variable even if I surround path_todb in
> > parenthesis
>
> No, you are not. The parentheses around $path_todb make it be assigned
> the return value from m// in list context. Have you read about the m//
> operator in "perldoc perlop"?
>
> "If the /g option is not used, m// in list context returns a list
> consisting of the subexpressions matched by the parentheses in the
> pattern, i.e., ($1, $2, $3...). (Note that here $1 etc. are also set,
> and that this differs from Perl 4's behavior.) When there are no
> parentheses in the pattern, the return value is the list (1) for success."
>
> In other words, there are two possible changes you can do to make it
> return what you want:
>
> - use the /g modifier, or
> - use capturing parentheses in the regex
>
> -- 
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to