in reply to Array reference
G'day rkk,
Welcome to the monastery.
You seem to have got off on the wrong foot somewhat. Here's a few tips:
Here's the guts of the code I might have used to tackle this problem:
#!/usr/bin/env perl -l use strict; use warnings; my $first; print join ' ' => @$_ for grep { $_->[-1] - $first <= 500 } sort { $b->[-1] <=> $a->[-1] } map { $first = $_->[-1] unless $first; $_ } map { [ split ] } <DATA>; __DATA__ ID1 ch1 70 mir abc xyz ch2 2050 ID4 ch1 120 mir abc xyz ch2 2025 ID2 ch1 90 mir abc xyz ch2 4000 ID3 ch1 100 mir abc xyz ch2 2045
After excluding the ID2 line, your data was already sorted. I've changed the order of the lines under __DATA__ to show that the sort works.
Here's the output:
$ pm_sort_exclude.pl ID1 ch1 70 mir abc xyz ch2 2050 ID3 ch1 100 mir abc xyz ch2 2045 ID4 ch1 120 mir abc xyz ch2 2025
Update: I left the 500 out of the comparison. Fixed: s/$_->[-1] <= $first/$_->[-1] - $first <= 500/
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array reference
by rkk (Novice) on Jun 13, 2013 at 15:10 UTC |