What you're asking for is a way to gain easy lookups, where the keys of hash1 refer to the same values that are the keys of hash2, and vice versa.

I can see the logic here. Hash lookups are O(1) in speed efficiency, whereas greping for a value is O(n). But the problem is that maintaing a pair of hashes that crossreference each other gains speed at the cost of memory.

When you find yourself in such a situation, it is often time to start thinking in terms of a relational database. Even if all you have are two columns, it makes sense to let the database engine deal with quickly locating your information for you. Let's say you have the following database table:

name | title --------|---------------- bart | troublemaker homer | delinquent dad marge | enabling mother maggie | wise baby lisa | child prodigy

You could perform lookups by name by using an SQL query such as this one:

SELECT title FROM jobs WHERE name='bart'

Or you could let the database search based on the other column instead...

SELECT name FROM jobs WHERE title='troublemaker'

The database engine is usually pretty good at doing lookups in very rapid time. Perl makes database work easy (or at least possible) using the DBI module. DBI is the Perl-side interface to your database. It still needs an actual database too. A very simple, easy to use, easy to install, single-file kind of database will do, and for that, DBD::SQLite is a great tool.

I hope this alternate approach is helpful.


Dave


In reply to Re: two hashes occupying the same space by davido
in thread two hashes occupying the same space by jfroebe

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.