Comparing all hash keys to a string

D

dalyea

I frequently do something like this:

foreach my $key (keys %hash) {
if ($string=~/$key/i) {
do_something;
}
}

Is there an easy and fast and more compact way to compare all
the keys of a hash to a string? Something like:

my @found=grep /$_/ $string, keys %hash;

where @found would contain all the matching keys from the
hash found in $string.

Thanks, I've always wanted to find a better way to do this.
 
A

Anton Ramunda

I frequently do something like this:

foreach my $key (keys %hash) {
if ($string=~/$key/i) {
do_something;
}
}

Is there an easy and fast and more compact way to compare all
the keys of a hash to a string? Something like:

my @found=grep /$_/ $string, keys %hash;

where @found would contain all the matching keys from the
hash found in $string.

Thanks, I've always wanted to find a better way to do this.

What you try to do remembers me to a pointer function call like in C.

The best would to put the function address of "do_something();" function
as value in the key of your hash table:

Then instead of the foreach and if calls simply execute:

$key{$string}();

It would be more faster than to parse strings. But your string should
then match exactly the entry in the hash table.

I'm not sure if its this what you want.
 
T

Tad J McClellan

I frequently do something like this:

foreach my $key (keys %hash) {
if ($string=~/$key/i) {
do_something;
}
}

Is there an easy and fast and more compact way to compare all
the keys of a hash to a string? Something like:

my @found=grep /$_/ $string, keys %hash;

where @found would contain all the matching keys from the
hash found in $string.


my @found = map {$string =~ /$_/gi} keys %hash;
or
my @found = map {$string =~ /\b$_\b/gi} keys %hash;
 
B

Ben Bullock

my @found = map {$string =~ /$_/gi} keys %hash;
or
my @found = map {$string =~ /\b$_\b/gi} keys %hash;

This is a very compact solution. Another possibility which might be
faster if you have a lot of keys and the hash does not change is to build
a regular expression from the hash keys as follows:

#!/home/ben/software/install/bin/perl
use warnings;
use strict;
my %hash = qw/ba be bu be bo ca di du de do Monster Monkey Brad Pitt/;
# Long version
my @sortedbylength = sort {length($b) <=> length($a)} keys %hash;
my $matchingexpression = '('.join ("|", @sortedbylength).')';
print "$matchingexpression\n";
my $string = "ba bill bo brad tiny monster";
my @matches = ($string =~ /$matchingexpression/gi);
print "@matches\n";
# Shorter version
my $m='('.join ('|',(sort {length $b <=> length $a} keys %hash)).')';
my @matches2 = ($string =~ /$m/gi);
print "@matches2\n";
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top