in reply to Re: Max value
in thread Max value
Update: I guess I should have added some comments to this post. There are very tricky and obtuse looking sort algorithms. Don't mistake "shorter code" or more "complex code" for "better code". When working with a hash of say <50 keys, my advice is to go with simple code. I start thinking about whipping out the "Perl nuclear weapons" when sort size gets above 1,000.#!/usr/bin/perl use strict; use warnings; my %hashName = (a => 1, b => 2, c => 3, d=>3); #sort keys by descending order of their value my @keys = sort {$hashName{$b} <=> $hashName{$a}} keys %hashName; my $max = $hashName{$keys[0]}; while (my $key = shift @keys) { last if ($hashName{$key} < $max); print "$key=>$hashName{$key}\n"; } __END__ c=>3 d=>3
|
|---|