Home: Perl Programming Help: Beginner:
Search a Hash


Apr 18, 2001, 4:47 PM

Views: 280
Search a Hash

How do you search a hash using a regular expression?

I have a module that is passed a regular expression and I need to use that regular expression to search through an associative array and return another associative array with all matching entries in it. How do I do that?


Apr 19, 2001, 9:18 AM

Views: 270
Re: Search a Hash

Just so you know, you can pass compiled regular expressions (in 5.005) with the qr// quoting operator:

$regex = qr/:-\)/;  # makes a regex like (?-ismx::-\)) 

if ($text =~ /$regex/) { ... }

As far as searching a hash with a regex, you probably want to grep() the keys (or values) of the hash.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author