in reply to
Hash Question
use strict;
use warnings;
+
my @names = ('some','keys','are','redundant','keys');
my @values = (1, 3, 4, 5, 2);
my %h = ();
# so that the hash will be accessible outside the while loop
+
my $i = 0;
while( $i < 5 ) { # a foreach loop would've been better
+
my $name = $names[$i];
my $value = $values[$i];
unless( defined $h{$name} and $h{$name} > $value ) {
# either this is new key-value pair,
+
# or the value is bigger than what the hash
+
# has currently
+
$h{$name} = $value;
}
$i = $i + 1;
}
# now print the results
+
foreach my $key (keys %h) { # keys are in random order
print "$key,$h{$key}\n";
}
This little program produces the following output:
keys,3
redundant,5
are,4
some,1