A C programmer's well trained reflex:#!/usr/bin/perl -w use strict; open my $fh, '<', 'details.txt' or die $!; until(grep !defined, my @details = map scalar <$fh>, 1 .. 4) { print @details, "\n"; }
A LISP hacker's immediate reaction:#!/usr/bin/perl -w use strict; open my $fh, '<', 'details.txt' or die $!; my $i; my @details; while(<$fh>) { push @details, $_; next if ++$i % 4; print @details, "\n"; @details = (); }
#!/usr/bin/perl -w use strict; open my $fh, '<', 'details.txt' or die $!; sub read_lines { my ($fh, $amnt) = @_; return unless defined(my $line = <$fh>); return ( $line, $amnt > 1 ? read_lines($fh, $amnt - 1) : () ); } while(my @details = read_lines($fh, 4)) { print @details, "\n"; }
Makeshifts last the longest.
In reply to Re: Read File In Four-Line Chunks / TMTOWTDI / Golf
by Aristotle
in thread Read File In Four-Line Chunks / TMTOWTDI / Golf
by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |