in reply to max value in a hash
First, instead of:
you could write:my $i = 0; foreach (sort sortHash (keys (%hashName))) { if ($i == 0) { $max = $hashName{$_}; last } } # end-foreach
because there is no need for the $i when you just check for 0.foreach (sort sortHash (keys (%hashName))) { $max = $hashName{$_}; last; } # end-foreach
Second, even easier in Perl, you can index returned lists when you put them in ( ):
or simply by doing an array assignment:my $max = ( sort sortHash (keys (%hashName)) )[0]; # also possible: my $min = ( sort sortHash (keys (%hashName)) )[-1];
also the ( ) around and after keys aren't necessary and avoiding them would make the code more readable.my ($max) = sort sortHash (keys (%hashName));
|
|---|