Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: sorting headers in a file

by Lennotoecom (Pilgrim)
on Dec 13, 2013 at 07:57 UTC ( [id://1066999]=note: print w/replies, xml ) Need Help??


in reply to sorting headers in a file

one of ways to go:
$/ = '>or'; sub{$h{$1} = $2 if /(\d+)\n\n(\w+)/}->() foreach <DATA>; foreach (sort { $a<=>$b } keys %h){ print ">or$_\n\n$h{$_}\n\n"; } __DATA__ >or3 agagatgatagat >or10 aacctttagtag >or1 gtatatatata >or2 tactacatgagg
output
>or1 gtatatatata >or2 tactacatgagg >or3 agagatgatagat >or10 aacctttagtag

Replies are listed 'Best First'.
Re^2: sorting headers in a file
by hdb (Monsignor) on Dec 13, 2013 at 08:34 UTC

    I would write

    sub{$h{$1} = $2 if /(\d+)\n\n(\w+)/}->() foreach <DATA>;
    as
    /(\d+)\n+(\w+)/ and $h{$1} = $2 for <DATA>;

    which I think is more readible. The modification of the regex makes it a little more robust relative to the formatting of the data. You would also lose data if there are duplicate ">or..." bits and multiline sequences.

      thanks for corrections
      your code is beautiful
      gotta remember that construction

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found