in reply to Re: Basic arithmetic functions
in thread Basic arithmetic functions

Anything awk can do...

perl -lane '$s+=$F[2]}{print $s/$.'

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Basic arithmetic functions
by hossman (Prior) on Aug 08, 2002 at 18:32 UTC
    okay, i'll bite. why does this work?

    I mean, using an explicit END block i understand...

    perl -lane '$s+=$F[2]; END{print $s/$.;}'

    But what is it about your "}{" that makes the print part of an implicit END block? Is this documented behavior? or an unintenional artifact of the way the parser deals with -n ?

      Is this documented behavior? or an unintenional artifact of the way the parser deals with -n ?

      Both. :-) (But you have to read the documentation creatively.) From perlrun:

      -n causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk: LINE: while (<>) { ... # your program goes here }

      When you put a '}{' in the middle of that it has the effect of ending the while loop and beginning another block (which is then finished by the old end of the while loop's block.)

      I think I picked it up from someone's round of golf. I'd give credit if I knew to whom it was due.

      -sauoq
      "My two cents aren't worth a dime.";