Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Compare hash with arrays and print

by jwkrahn (Abbot)
on Jul 12, 2010 at 22:12 UTC ( [id://849120]=note: print w/replies, xml ) Need Help??


in reply to Re: Compare hash with arrays and print
in thread Compare hash with arrays and print

my %rhash = reverse %hash; my %fh; foreach my $num (keys %rhash)

Since you only need the unique keys from %hash you only really need an array:

my @keys = keys %{{ reverse %hash }}; my %fh; foreach my $num ( @keys )

Replies are listed 'Best First'.
Re^3: Compare hash with arrays and print
by Marshall (Canon) on Jul 12, 2010 at 23:47 UTC
    Correct. The code is basically equivalent. We both reverse the hash and then find the keys of that reversed hash. Making a named array @keys is actually an extra step. I suppose if we wanted to, the whole right hand side of the "my @keys" line could go into the foreach() statement but, that could be obtuse.

    I was trying to show the workings of the reverse in the most straightforward way possible (simple syntax). I updated post to show the Op how to easily use Dumper to print the reversed hash, which is little more obvious how to do when it has an assigned name (rhash).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2025-12-09 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (88 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.