in reply to How to print the 10 most recent (epoch) keys in a hash when epoch is not the key
Build an array of the keys ordered (descending) by the epoch; pop and delete until there are just 10 left; and use the remaining to output in order:
#! perl -slw use strict; use Data::Dump qw[ pp ]; my $now = time; my %hash = map{; "url$_" => { title => "title $_", EPOCH => $now - 50 + int( rand 100 ), DESC => "Desc $_ ", } } 1 .. 12; pp \%hash; my @epochOrderKeys = sort { $hash{ $b }{ EPOCH } <=> $hash{ $a }{ EPOCH } } keys %hash; delete $hash{ pop @epochOrderKeys } while @epochOrderKeys > 10; print "$_ : ", pp $hash{ $_ } for @epochOrderKeys; __END__ c:\test>809852 { url1 => { DESC => "Desc 1 ", EPOCH => 1259396887, title => "title 1 +" }, url10 => { DESC => "Desc 10 ", EPOCH => 1259396825, title => "title +10" }, url11 => { DESC => "Desc 11 ", EPOCH => 1259396896, title => "title +11" }, url12 => { DESC => "Desc 12 ", EPOCH => 1259396905, title => "title +12" }, url2 => { DESC => "Desc 2 ", EPOCH => 1259396822, title => "title 2 +" }, url3 => { DESC => "Desc 3 ", EPOCH => 1259396833, title => "title 3 +" }, url4 => { DESC => "Desc 4 ", EPOCH => 1259396873, title => "title 4 +" }, url5 => { DESC => "Desc 5 ", EPOCH => 1259396824, title => "title 5 +" }, url6 => { DESC => "Desc 6 ", EPOCH => 1259396833, title => "title 6 +" }, url7 => { DESC => "Desc 7 ", EPOCH => 1259396827, title => "title 7 +" }, url8 => { DESC => "Desc 8 ", EPOCH => 1259396910, title => "title 8 +" }, url9 => { DESC => "Desc 9 ", EPOCH => 1259396843, title => "title 9 +" }, } url8 : { DESC => "Desc 8 ", EPOCH => 1259396910, title => "title 8" } url12 : { DESC => "Desc 12 ", EPOCH => 1259396905, title => "title 12" + } url11 : { DESC => "Desc 11 ", EPOCH => 1259396896, title => "title 11" + } url1 : { DESC => "Desc 1 ", EPOCH => 1259396887, title => "title 1" } url4 : { DESC => "Desc 4 ", EPOCH => 1259396873, title => "title 4" } url9 : { DESC => "Desc 9 ", EPOCH => 1259396843, title => "title 9" } url3 : { DESC => "Desc 3 ", EPOCH => 1259396833, title => "title 3" } url6 : { DESC => "Desc 6 ", EPOCH => 1259396833, title => "title 6" } url7 : { DESC => "Desc 7 ", EPOCH => 1259396827, title => "title 7" } url10 : { DESC => "Desc 10 ", EPOCH => 1259396825, title => "title 10" + }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to print the 10 most recent (epoch) keys in a hash when epoch is not the key
by ikegami (Patriarch) on Nov 28, 2009 at 17:48 UTC | |
|
Re^2: How to print the 10 most recent (epoch) keys in a hash when epoch is not the key
by kevyt (Scribe) on Nov 28, 2009 at 15:25 UTC |