case insensitive hash lookup

J

Jack

Hello,

I noticed the hash lookup code below doesnt allow for case insensitive
nor pattern match lookups.. I want to essentially be able to do the
same as this (notice the "i"):
if ($temp2 =~ m/Abdomen/i) { print " case insensitive pattern matched
! "; }
with my hash lookup:
if (exists $hash{$temparray2[$sourcefield]}) { print " exact match
found only " }

Does anyone know how to accomplish this.

Thank you,

Jack
 
J

Jürgen Exner

Jack said:
I noticed the hash lookup code below doesnt allow for case insensitive
nor pattern match lookups..[...]

Does anyone know how to accomplish this.

Simple, just use a normal form, e.g. all upper case or all lower case as the
key.

jue
 
J

Jack

Aukjan said:
Jack said:
Hello,

I noticed the hash lookup code below doesnt allow for case insensitive
nor pattern match lookups.. I want to essentially be able to do the
same as this (notice the "i"):
if ($temp2 =~ m/Abdomen/i) { print " case insensitive pattern matched
! "; }
with my hash lookup:
if (exists $hash{$temparray2[$sourcefield]}) { print " exact match
found only " }

If you just wish to use the lowercase value as a hash key, you can use
the Perl function 'lc' ( perldoc -f lc ). This will return the lowercase
of the value given:

if ( exists $hash{ lc( $temparray2[$sourcefield] ) } {
print " Lowercase key exists in hash";
}


Aukjan

thanks but this does not cover mixed case in the word, nor pattern
matching within the hash lookup - how can this be tested ?? ... if I
were to iterate through an array I could easily do this, but thats
slower than a hash lookup

Jack
 
A

anno4000

Jack said:
Jack said:
Hello,

I noticed the hash lookup code below doesnt allow for case insensitive
nor pattern match lookups.. I want to essentially be able to do the
same as this (notice the "i"):
if ($temp2 =~ m/Abdomen/i) { print " case insensitive pattern matched
! "; }
with my hash lookup:
if (exists $hash{$temparray2[$sourcefield]}) { print " exact match
found only " }

If you just wish to use the lowercase value as a hash key, you can use
the Perl function 'lc' ( perldoc -f lc ). This will return the lowercase
of the value given:

if ( exists $hash{ lc( $temparray2[$sourcefield] ) } {
print " Lowercase key exists in hash";
}


Aukjan

thanks but this does not cover mixed case in the word,

It does, if you lower-case all keys before storing.
nor pattern
matching within the hash lookup - how can this be tested ??

A hash key is always a specific string and lookup is always for
that exact key. This is the basic operation of the data structure
called a hash. "Pattern matching within the hash lookup" is a
contradiction in terms.
... if I
were to iterate through an array I could easily do this, but thats
slower than a hash lookup

A hash is as fast as it is *because* it only has one key (or a very
limited set of keys) to consider in each lookup. Thus the access time
can be kept independent of the number of keys. If all keys must be
considered, you lose that property willy-nilly.

Anno
 
U

usenet

Jack said:
I noticed the hash lookup code below doesnt allow for case insensitive
nor pattern match lookups..

Tie::CPhash will let you have a case-preserving hash with
case-insensitive lookups (if you must). But general pattern matching
on hash keys is a nuther thing...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top