I am facing problems with below code. This works with my search criteria 'Technology Phones' and returns correct key "TEC-PH 10001949" which is my requirements.

#! perl use strict; use warnings; use Data::Dumper; my %categories = ("FUR-BO 10001798" => 'Furniture', "FUR-CH 10000454" => [ 'Furniture' , 'Chairs'], "OFF-LA 10000240" => [ 'Office Supplies' , 'Labels'] +, "OFF-ST 10000107" => [ 'Office Supplies' , 'Storage' +], "OFF-AR 10003056" => [ 'Office Supplies' , 'Art'], "TEC-PH 10001949" => 'Technology Phones', ); #print Dumper(%categories); print "What is Category code for Technology Phones | Output s +hould be : TEC-PH 10001949? \n"; my $categorycode; my $search = 'Technology Phones'; my @category = (); foreach $categorycode (keys(%categories)) { if(ref($categories{$categorycode}) eq 'ARRAY' and grep { $_ eq $search } @{ $categories{$categorycode} }) { push(@category, $categorycode); } elsif(!ref($categories{$categorycode}) and $categories{$category +code} eq $search) { push(@category, $categorycode); } } print join(", ", @category) . "\n";

However if I change the search = 'Furniture' I need the output as "FUR-BO 10001798" But I get key value "FUR-BO 10001798" & "FUR-CH 10000454" which is not correct.

Also If I give search criteria as 'Office Supplies' , 'Labels' I want my output as "OFF-LA 10000240".

## Facing problems for below serach and desired output print "What is Category code for Furniture | Output s +hould be : FUR-BO 10001798? \n"; print "What is Category code for (Furniture Chairs) | Output s +hould be : OFF-LA 10000240? \n"; print "What is Category code for (Office Supplies Storage) | Output s +hould be : OFF-ST 10000107? \n"; print "What is Category code for (Office Supplies Art) | Output s +hould be : OFF-AR 10003056? \n"; print "What is Category code for (Office Supplies Labels) | Output s +hould be : OFF-LA 10000240? \n";

So $search variable are value/values in my hash and I need to return key as output.

Please help on fixing this. Thanks.


In reply to return Hash key while hash value/values is search criteria by Magnolia25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.