SysAdm has asked for the wisdom of the Perl Monks concerning the following question:

I have a Hash list :
%category = ( Anim => 'Animation Projects', Build => 'Building/Structural Projects', CAD => 'CAD/CAM Engineering Projects', );

Now I would like to search the KEYS and compare to a $variable, and print the VALUE for the matched $variable.

Another words compare ($variable = Anim) to the Hash and print just the value for that KEY (Animation Projects)

I keep printing every VALUE when it finds a match, not just the matched VALUE..

Thanks in advance...

______SysAdm

Replies are listed 'Best First'.
Re: Matching a KEY in a HASH
by merlyn (Sage) on May 14, 2001 at 19:42 UTC
      I must not be making myself clear...

      The $variable = "" could = Anim or CAD or anything in the list...

      My question is ... How do I search the HASH to match the $variable, but print the VALUE only...

      make sense?
        I must not be making myself clear.

        The point of a hash is that a hash itself can find a value for a given key trivially.

        $value_you_are_looking_for = $category{$variable};
        There's no searching required. If there's an element with a key of $variable, then $value_you_are_looking_for has it. Otherwise, it gets undef.

        -- Randal L. Schwartz, Perl hacker

Re: Matching a KEY in a HASH
by larsen (Parson) on May 14, 2001 at 19:47 UTC
    The purpose of a hash (among other things) is avoiding exaustive searches. So you could write something like:
    $category{'Anim'};
    in order to obtain Animation Projects. I suggest reading perldata.

    Also, if you want to match keys using a regular expression, read this (Tie::Hash::Regex by davorg).

Re: Matching a KEY in a HASH
by flocto (Pilgrim) on May 14, 2001 at 20:55 UTC
    Ok, the other comments obviously misunderstood what your point was, but don't worry, it's still trivial:
    my %hash = ( 'Anim' => 'Animation', ... 'Test' => 'Whatever'); my $key = 'Anim'; print $key if $hash{$key};
    Regards, octo
    --
    GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
Re: Matching a KEY in a HASH
by suaveant (Parson) on May 14, 2001 at 21:23 UTC
    You want to match a key based on a substring? and print all values that match? You can use the Tie::Hash mentioned above or just to match a substring you can do...
    for(keys %category) { print "$category{$_}\n" if /\Q$variable\E/i; }
    the \Q \E makes sure that you match the characters in $variable the way they are, (other a \ could screw you all up, or a . or a *, etc) and the i at the end makes it case-insensitive (often good in substring matching). Does that help?
                    - Ant
      Here's the code I'm currently using:
      %category = ( Anim => 'Animation Projects', Build => 'Building/Structural Projects', CAD => 'CAD/CAM Engineering Projects', ); my $value; $value = (sort values %category); print "$value" if (exists($category{"$form{'USER_INPUT'}"})) ;

      It prints EVERY $value instead of just the $value for the matching $form{'USER_INPUT'}.

      I hope my question is now clearer and makes more sense..
        Yes, stop getting all the values, when all you want is one value. Use the code I've posted twice in this thread, as have others.

        -- Randal L. Schwartz, Perl hacker

        Ummm, first things first...
        $value = (sort values %category);
        will store the last value of the sorted list of values in $value, this is most likely not what you want. You are looking to print the value of %category that the key matches $form{'USER_INPUT'}? then use..
        print $category{$form{'USER_INPUT'} if $category{$form{'USER_INPUT'};
        do you just want the value of Anim if the user inputs Anim, or do you want Build AND CAD when the user inputs 'd'?
                        - Ant

        As merlyn said, looks like you are trying to grab all the 'values' with the $value = (sort values %category); statement.

        If you just use

        print "$category{$form{'USER_INPUT'}}" if exists $category{$form{'USER_INPUT'}};

        everything should work fine. (At least it did for me... :)

        D a d d i o