Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Looking for elegance

by hdb (Monsignor)
on Jan 27, 2014 at 13:27 UTC ( [id://1072222]=note: print w/replies, xml ) Need Help??


in reply to Looking for elegance

I prefer a line-by-line approach and create a new array each time a line starting with a number is encountered. I assume that the data fits into memory, otherwise sorting will be difficult:

use strict; use warnings; my @lines; while(<DATA>){ push @lines, [$1] if /^(\d+)/; push @{$lines[-1]}, $_; } print @$_[1..@$_-1] for sort { $a->[0] <=> $b->[0] } @lines; __DATA__ 2 [...] [line1] [line2] [line3] 3 [...] [line1] 1 [...] [line1] [line2] 2 [...] [line1] [line2]
Update: @lines should really be called @blocks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found