Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-21 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls