Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Read File In Four-Line Chunks / TMTOWTDI / Golf (more)

by Aristotle (Chancellor)
on Jun 06, 2003 at 14:55 UTC ( [id://263697]=note: print w/replies, xml ) Need Help??


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

Some further perversions of the LISPish way:
#!/usr/bin/perl -w use strict; open my $fh, '<', 'details.txt' or die $!; my $read_lines; while( my @details = ($read_lines = sub { my $amnt = shift; return unless defined(my $line = <$fh>); return ( $line, $amnt > 1 ? $read_lines->($amnt - 1) : () ); })->(4) ) { print @details, "\n"; }
Refined perversion:
#!/usr/bin/perl -w use strict; sub make_read_lines { my ($fh, $lines) = @_; return $lines == 1 ? sub { return unless defined(my $line = <$fh>); $line } : do { my $next_lines = make_read_lines($fh, $lines - 1); sub { return unless defined(my $line = <$fh>); $line, $next_lines->() }; }; } open my $fh, '<', 'details.txt' or die $!; my $read_4_lines = make_read_lines($fh, 4); while(my @details = $read_4_lines->()) { print @details, "\n"; }

Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://263697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-16 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found