Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search hash keys

Status
Not open for further replies.

al0

Technical User
Apr 30, 2006
6
SE
Hi,

I have made a data structure that consists of hash and the keys referencing to arrays. The key names comes from the first elements in the array it referencing to. The thing I trying to accomplish is to be able to search trough all keys and print all that matches the string. From the keys that get listed from the search in the program I want to be able to get more detail about the key, by going into to that key's array.

I guess regular expressions would work for this but I dont know exactly where to begin.

/ Olaus
 
You can get the matching keys like so:
Code:
my @matching_keys = grep $_ eq "what you're looking for", keys %hash;
 
This works only if you type the exact keyname. I want to be able to search for example "test" and get all keys that consist of the word "test".
 
The "exact keyname" and "keys that consist of the word 'test'" are the same thing, no?

Perhaps you mean keys that *contain* the word test?
 
Yes I mean contain, pardon my english. So if I have these keys for example:

sometest
some test
testing
other keys

If I search for "test" I want to get the first three keys.
 
to borrow from ishnids code:

Code:
my @matching_keys = grep {/test/} keys %hash;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top