ryddler has asked for the wisdom of the Perl Monks concerning the following question:
The other day I ran across a scenario where I needed to be able to look up data in a hash by both keys and values. One of the databases we use stores a county as a number field. Some of the legacy software we have that uses the database stores a char array to translate for use in the program. I was writing a web interface to do a search, and needed a way to search the county field by name as opposed to number. After a little dig through the docs, a few suggestions from some people here, and some skull scratching I determined the best course of action in my case since all my values were unique, was to store both names and numbers as keys with the appropriate values like below.
my($count, %il_counties ); my @il_counties = ("blank","ADAMS","ALEXANDER","BOND","BOONE","BRO +WN","BUREAU","COOK","CALHOUN", "CARROLL","CASS","CHAMPAIGN","CHRISTIAN","CLARK","CLAY","CLINT +ON","COLES", "CRAWFORD","CUMBERLAND","DE KALB","DE WITT","DOUGLAS","DUPAGE" +,"EDGAR", "EDWARDS","EFFINGHAM","FAYETTE","FORD","FRANKLIN","FULTON","GA +LLATIN", "GREENE","GRUNDY","HAMILTON","HANCOCK","HARDIN","HENDERSON","H +ENRY","IROQUOIS", "JACKSON","JASPER","JEFFERSON","JERSEY","JO DAVIESS","JOHNSON" +,"KANE","KANKAKEE", "KENDALL","KNOX","LAKE","LA SALLE","LAWRENCE","LEE","LIVINGSTO +N","LOGAN", "MACON","MACOUPIN","MADISON","MARION","MARSHALL","MASON","MASS +AC","MCDONOUGH", "MCHENRY","MCLEAN","MENARD","MERCER","MONROE","MONTGOMERY","MO +RGAN","MOULTRIE", "OGLE","PEORIA","PERRY","PIATT","PIKE","POPE","PULASKI","PUTNA +M","RANDOLPH", "RICHLAND","ROCK ISLAND","ST. CLAIR","SALINE","SANGAMON","SCHU +YLER","SCOTT", "SHELBY","STARK","STEPHENSON","TAZEWELL","UNION","VERMILION"," +WABASH","WARREN", "WASHINGTON","WAYNE","WHITE","WHITESIDE","WILL","WILLIAMSON"," +WINNEBAGO", "WOODFORD" ); $il_counties{$_} = $count++ foreach (@il_counties); $il_counties{$il_counties{$_}} = $_ foreach (keys %il_counties);
By doing this I was able to just plug in either the number of the county or the name, and get back the corresponding data, but again, all my values were unique, so this wouldn't work with non-unique values.
I'm wondering if anyone else has run across the need to retrieve the hash key from a known value. How did you do it? Or if you haven't, how would you do it?
ryddler
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Finding a hash key from the value (reverse lookup)
by davorg (Chancellor) on Jan 22, 2001 at 19:03 UTC | |
Re: Finding a hash key from the value (reverse lookup)
by kilinrax (Deacon) on Jan 22, 2001 at 18:36 UTC | |
by PsychoSpunk (Hermit) on Jan 22, 2001 at 23:34 UTC | |
by Anonymous Monk on Jan 23, 2001 at 02:23 UTC | |
by PsychoSpunk (Hermit) on Jan 23, 2001 at 04:24 UTC | |
Re: Finding a hash key from the value (reverse lookup)
by PsychoSpunk (Hermit) on Jan 22, 2001 at 23:15 UTC | |
Re: Finding a hash key from the value (reverse lookup)
by Beatnik (Parson) on Jan 22, 2001 at 18:49 UTC |