http://www.perlmonks.org?node_id=44329


in reply to Hash slices ?

The original discussion for this one
started with my question: how do I find if
at least one of many keys in a list is in a hash
(quick, fast, and simple)
davorg offered me a working example, but when I
tried it, I inserted the 'four' at the start of the list
which didn't work
The push on the other hand DID.
and as you can see the unshift doesn't work either

Many thanks davorg !!
Let's see what people can tell us about this one ;))

Replies are listed 'Best First'.
Re: Re: Hash slices ?
by arturo (Vicar) on Dec 01, 2000 at 18:58 UTC
    how do I find if at least one of many keys in a list is in a hash (quick, fast, and simple)

    Dunno if this meets *all* of your criteria, but I find this simple enough:

    my @search_keys = qw(one two three); my %hash = ( four=>4, five=>5, six=>6); my $foundit =0; foreach (@search_keys) { if (exists $hash{$_}) { $foundit =1; print "found an entry for $_ in %hash!\n"; } }

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      Given that the idea is to find out if any of the keys exist in the hash, it would be more efficient to call last and exit the loop once you've found one.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me