On Friday 07 December 2007 07:13, Rodrick Brown wrote:
>
> On Dec 7, 2007 9:45 AM, John W. Krahn <[EMAIL PROTECTED]> wrote:
> >
> > On Friday 07 December 2007 06:36, Rodrick Brown wrote:
> > >
> > > perl -nle '$total+= (split/\s+/,$_)[4]; END { printf "Avg:
> > > %.2f\n", $total/$. } ' /tmp/filename
> >
> > perl -ane'$total+=$F[4]}{printf"Avg: %.2f\n",$total/$.'
> > /tmp/filename
>
> Hi John please explain how exactly $F[4] works? how is it set?
perldoc perlrun
[ SNIP ]
-a turns on autosplit mode when used with a -n or -p.
An implicit split command to the @F array is done as
the first thing inside the implicit while loop
produced by the -n or -p.
perl -ane 'print pop(@F), "\n";'
is equivalent to
while (<>) {
@F = split(' ');
print pop(@F), "\n";
}
An alternate delimiter may be specified using -F.
> also the }{ brackets???
-n creates the while loop and the }{ closes the while loop and encloses
the printf inside brackets on its own.
while ( <> ) {
$total += $F[4]
}
{
printf"Avg: %.2f\n",$total/$.
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/