I need to find the max value of all values in a Perl hash. I don't need the key, just the highest value, so that I can increase it and return a new value that is higher than before. Simplest thing in the world. I took inspiration from this answer: https://stackoverflow.com/a/2891180/2740187, and implemented this piece of code:
use List::Util qw( reduce min max );
my %gid = ("abc" => 1, "def" => 1);
my $gid_ref = \%gid;
my $max_gid = reduce { $a > $b ? $a : $b } values %$gid_ref || 0;
print "$max_gid\n";
As you can see, the hash only contains two 1's as values. Then why is it printing "2"? At least it does on my machine.
I guess I just could write
my $max2 = max(values %$gid_ref) || 0;
to get the max value anyway, but I really would like to understand what is happening here.