Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Hash Question

by tford (Beadle)
on Nov 23, 2009 at 01:57 UTC ( #808712=note: print w/ replies, xml ) Need Help??

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

Comment on Re: Hash Question
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://808712]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-12-01 03:53 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (792 votes), past polls