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


In reply to Finding a hash key from the value (reverse lookup) by ryddler

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.