Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: return Hash key while hash value/values is search criteria

by poj (Abbot)
on Jul 01, 2018 at 15:51 UTC ( [id://1217703]=note: print w/replies, xml ) Need Help??


in reply to return Hash key while hash value/values is search criteria

Build a 'reverse' hash of category to code.

#!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', ); # build reverse hash text=>code my %code = (); for my $code (keys %categories){ my $cat = $categories{$code}; if ( ref($cat) eq 'ARRAY' ){ $cat = join ' ',@$cat; } if (exists $code{$cat}){ die "Duplicate code for $cat\n"; } else { $code{$cat} = $code }; } print Dumper \%code; my @items = ('Furniture', 'Furniture Chairs', 'Office Supplies Storage', 'Office Supplies Art', 'Office Supplies Labels'); search($_) for @items; sub search { my $text = shift; printf "Code for %-30s is %s\n",$text,$code{$text}; }
poj

Replies are listed 'Best First'.
Re^2: return Hash key while hash value/values is search criteria
by Magnolia25 (Sexton) on Jul 03, 2018 at 03:11 UTC

    Thanks it works

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-26 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found