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

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 ?

Replies are listed 'Best First'.
Re: Re: Re: Re: Basic arithmetic functions
by sauoq (Abbot) on Aug 08, 2002 at 18:54 UTC
    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.";