Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Array reference

by hbm (Hermit)
on Jun 12, 2013 at 23:55 UTC ( #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? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-09-28 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?