Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Array reference

by hbm (Hermit)
on Jun 12, 2013 at 23:55 UTC ( [id://1038624]=note: print w/replies, xml ) Need Help??


in reply to Array reference

Classic map-sort-map:

  1. Turn each line into an anonymous array with two items - the last column, and the full line.
  2. Sort those arrays by their first element (the last column).
  3. Print just the second element (the full line) for each anonymous array.

The only trick is, in #1, keep track of the smallest last column.

my $least; print map { abs($_->[0]-$least)<500 && $_->[1] } sort{ $a->[0] <=> $b->[0] } map { /(\d+)$/; $least = $1 if !defined $least or $1<$least; [ $1, $_] } <DATA> __DATA__ ID1 ch1 70 mir abc xyz ch2 2050 ID2 ch1 90 mir abc xyz ch2 4000 ID3 ch1 100 mir abc xyz ch2 2045 ID4 ch1 120 mir abc xyz ch2 2025 # prints: ID4 ch1 120 mir abc xyz ch2 2025 ID3 ch1 100 mir abc xyz ch2 2045 ID1 ch1 70 mir abc xyz ch2 2050

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found