Any Perl hackers out there?

Posted:
in Genius Bar edited January 2014
Is there any way to do a case-insensitive hash lookup in Perl?



I'm writing a program that compares two lists and spits out the number of shared elements. I use a standard hash lookup to do that:

Code:


foreach $libgene (@libvalues) {

$libcount++ if exists $list{$libgene};

} # close foreach





@libvalues is one list (actually the values of a hash), and %list is the other list (hash). $libcount is the number of shared elements between them.



In order to make the program easier to use, though, I'd like to make the hash lookup insensitive to case, so the user doesn't have to fret about whether his two lists are uppercase or lowercase, or some combination thereof. Is there any way to reasonably do this?



One way I came across on Google was to use grep instead of a hash lookup, like so:

Code:


foreach $listgene (@listkeys) {

$matches = 0;

$matches = grep /^$listgene$/i, @libvalues;

$libcount = $libcount + $matches;

} # close foreach





It works, but it's god-awful slow. The hash-lookup-using script executes a certain set of arguments in 15 seconds. The grep version, with all other code the same and the same arguments, took over 15 minutes. A sixty-fold decrease in performance is not worth case insensitivity. If I didn't appreciate the beauty of hash lookups before, I certainly do now. So is there anyway to do a case-insensitive hash lookup?

Comments

  • Reply 1 of 2
    have you timed a scheme where you duplicate one of the lists but convert it to all upper/lower case and then when doing the hash lookup converting the lookup string to upper/lower case on the fly?



    Otherwise, I don't know of any way to be case insensitive...
  • Reply 2 of 2
    toweltowel Posts: 1,479member
    Quote:

    Originally posted by KWSN_hgs

    have you timed a scheme where you duplicate one of the lists but convert it to all upper/lower case and then when doing the hash lookup converting the lookup string to upper/lower case on the fly?



    Otherwise, I don't know of any way to be case insensitive...




    That's exactly what I settled on, for now. Everything is converted on-the-fly to uppercase while it's being loaded into the lists. It works; the user can use any combination of case and the lookups will work as intended. And it's fast, adding almost nothing to the execution time. And it only took a few extra lines of code, one above each "push onto list" statement. It still seems a little kludgy to my newbie mind, but maybe that's how case-insensitivity works in the real-world.
Sign In or Register to comment.