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

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.";

Replies are listed 'Best First'.
Re^3: Read File In Four-Line Chunks / TMTOWTDI / Golf (bug with EOL-less last line)
by Aristotle (Chancellor) on Jun 07, 2003 at 10:23 UTC
    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.