in reply to Read File In Four-Line Chunks / TMTOWTDI / Golf
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Read File In Four-Line Chunks / TMTOWTDI / Golf (more)
by Aristotle (Chancellor) on Jun 06, 2003 at 14:55 UTC |