Do you need to preserve the case of the keys? It might be easiest to convert all keys to lowercase in the hash and then lowercase the keys whenever you do any lookup.

If you do have to keep the case you might create a lookup hash that would just map from the lowercased keys to the original ones and then do a two-step lookup:

my %alt_seqindex; $alt_seqindex{lc $key} = $key while (my ($key) = each %seqindex);
and then do the case insensitive lookup like this:
$value = $seqindex{$alt_seqindex{lc $key}};
If you do want to hide this from the users you may subclass BerkeleyDB::Hash, somewhat like this:
package BerkeleyDB::Hash::CasePreserve; use BerkeleyDB; use base 'BerkeleyDB::Hash'; my %LCKeys; # to keep the lc $key => $key mapping for all hashes sub TIEHASH { my $obj = &BerkeleyDB::Hash::TIEHASH; my %lckeys; my $key = $obj->FIRSTKEY(); $lckeys{lc $key} = $key; $lckeys{lc $key} = $key while $key = $obj->NEXTKEY($key); $LCKeys{$obj->[0]} = \%lckeys; return $obj; } sub FETCH { my $self = shift; my $key = shift(); $key = $LCKeys{$self->[0]}->{lc $key} or return; return $self->SUPER::FETCH($key); } sub EXISTS { my $self = shift; my $key = shift(); $key = $LCKeys{$self->[0]}->{lc $key} or return; return $self->SUPER::EXISTS($key); } sub STORE { my $self = shift; my ($key, $value) = @_; if (exists $LCKeys{$self->[0]}->{lc $key}) { $key = $LCKeys{$self->[0]}->{lc $key}; } else { $LCKeys{$self->[0]}->{lc $key} = $key; } return $self->SUPER::STORE($key, $value); } sub DESTROY { my $self = shift(); delete $LCKeys{$self->[0]}; } 1;
This implementation will be much quicker than Fletch's since it doesn't search through the whole tied hash each and every time.

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne


In reply to Re: Tie one hash two ways? by Jenda
in thread Tie one hash two ways? by MrMadScience

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.