Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://808712]
Discipulus seems a part of the globe very unfrindly with Kawasaki riders..
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)
[perldigious]: "for how I roll"... ahhh, I see what you did there stevieb.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2016-12-06 16:52 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.