FAQ
I'm trying to learn hashes to incorporate into many places where arrays
seem a little too "bulky" but I've got a question regarding returning a
single key when I know a single value in the hash.

I've figured out the following (feel free to correct if I've got something
wrong):

my %test=(joe,smith,mike,garner);
@fnames=keys(%test); #all the first names, the keys
@lnames=values(%test); # all last names, the values

$mike_last_name=$test{"mike"}; #returns 1 value when I know the key.

How do I do the opposite of the above statement? How do I say:
Return the first name (key) for the person who's last name (value) is
"garner" ?
I think I figured out one way but I'm just curious to know if I've missed
something and there's a better way to do it without creating a second hash
like this:
%test2= reverse %test;
$garner_last_name=$test2{"garner"};

Any ideas? Or is this exactly how it should be done?

Thanks in advance.
Mike



--------------------------------------------------------------
Mike Garner
Western State College
Email: [email protected]
Voice: 970-943-3123
Fax: 970-943-7069

Search Discussions

  • Wiggins d'Anconia at May 16, 2003 at 12:50 am

    Mike Garner wrote:
    I'm trying to learn hashes to incorporate into many places where arrays
    seem a little too "bulky" but I've got a question regarding returning a
    single key when I know a single value in the hash.

    I've figured out the following (feel free to correct if I've got
    something wrong):

    my %test=(joe,smith,mike,garner);
    @fnames=keys(%test); #all the first names, the keys
    @lnames=values(%test); # all last names, the values

    $mike_last_name=$test{"mike"}; #returns 1 value when I know the key.

    How do I do the opposite of the above statement? How do I say:
    Return the first name (key) for the person who's last name (value) is
    "garner" ?
    I think I figured out one way but I'm just curious to know if I've
    missed something and there's a better way to do it without creating a
    second hash like this:
    %test2= reverse %test;
    $garner_last_name=$test2{"garner"};

    Any ideas? Or is this exactly how it should be done?
    Interesting, wouldn't have thought of that. Have a look at:

    perldoc -f reverse

    For reasons why this may get you into trouble. There are many ways to do
    this (naturally), have a look at:

    perldoc -f grep
    perldoc -f map

    For functions taht can be used along side of the keys/values you already
    mentioned. And there is the verbose way:

    foreach my $key (keys(%test)) {
    if ($test{$key} eq 'garner') {
    $first_name = $key;
    }
    }

    http://danconia.org
  • R. Joseph Newton at May 17, 2003 at 5:36 pm

    Mike Garner wrote:

    I'm trying to learn hashes to incorporate into many places where arrays
    seem a little too "bulky" but I've got a question regarding returning a
    single key when I know a single value in the hash.
    I'm going to break right here, because you need to re-examine this construct.
    The return for a single value in a hash is not a single scalar, but a list.
    For any possible value, in fact, the return is a list, though an empty list in
    most cases. Since there is no requirement of unique values in a hash, the
    occurence of one-to-one relationships is only lucky coincidence.

    Joseph

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 15, '03 at 10:00p
activeMay 17, '03 at 5:36p
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase