in reply to Read File In Four-Line Chunks / TMTOWTDI / Golf

Untested code which should not be sprung on unwary maintainance programmers:
while(grep {$_=<FH>; chop} my($name,$address,$phone,$fax) ) { # Do stuff }
(No Perl here, I can't test it. And I will hang my head in shame if it doesn't work...)

Replies are listed 'Best First'.
Re^2: Read File In Four-Line Chunks / TMTOWTDI / Golf (don't chop)
by Aristotle (Chancellor) on Jun 06, 2003 at 11:50 UTC
    Cute (though no Perl here either), but you can chomp and it'll work the same, while that chop is dangerous.

    Makeshifts last the longest.

Re: Re: Read File In Four-Line Chunks / TMTOWTDI / Golf
by sauoq (Abbot) on Jun 06, 2003 at 23:56 UTC

    Nice. You can prevent it from dealing with partial records at the end of the input too...

    while ( 4 == scalar grep {$_ = <FH>; chomp} my($name,$address,$phone,$ +fax) ) { # Do stuff }

    Update: See Aristotle's comments below. This will fail to process a four line record if the last record is missing an EOL. Also, using an explicit scalar is superfluous... and if you are already torturing your maintenance programmer with this construct, why be explicit? :-)

    -sauoq
    "My two cents aren't worth a dime.";
    
      Careful - if the last line of the file has no newline, chomp will return 0, causing grep to return 3, rather than 4, causing the while to terminate early even though the record is complete. Also, you don't need the explicit scalar - == provides scalar context for you (case in point: @a == @b checks for identical array size).
      while(4 == grep { chomp($_ = <FH>); defined } my($name,$address,$phone +,$fax)){ # Do stuff }

      Makeshifts last the longest.