2007/5/4, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
Hello guys, I can't find the solution for sorting a two-dimensional hash. I'm sure some of you can help me. The first dimension of the hash has filenames in it, the second consists of two keys, 'data' and 'lines'. I want to sort the whole thing by a numerical comparison on 'lines' and tried this: %toc = sort { print $toc{$a}{'lines'}."\n"; $toc{$a}{'lines'} <=> %$toc{$b}{'lines'}; } %toc; Any hints?
Hello, See example codes below you may get some hints. use strict; use Data::Dumper; my %hash = ( 'file1' => { data => '...', lines => 33, }, 'file2' => { data => '...', lines => 11, }, 'file3' => { data => '...', lines => 44, }, 'file4' => { data => '...', lines => 22, }, 'file5' => { data => '...', lines => 10, }, ); for (sort { $hash{$a}->{lines} <=> $hash{$b}->{lines} } keys %hash) { print Dumper $hash{$_}; } __END__ If you need to sort them by lines desc,then do, for (sort { $hash{$b}->{lines} <=> $hash{$a}->{lines} } keys %hash) { ... } -- Chinese Practical Mod_perl book online http://home.arcor.de/jeffpang/mod_perl/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/