my @kwlist = qw(foo bar baz qux); my $kwre = join('|',@kwlist); my @items = getfromdb("select id,text from table"); # I have a sub that does this foreach my $i (0..$#items) { my ($id,$text) = @{$items[$i]}; my $count = 0; while($text =~ /$kwre/g) { $count++; } } #### use JSON::XS; my @kwlist = qw(foo bar baz qux); my @items = getfromdb("select id,keywordhash from table"); # I have a sub that does this foreach my $i (0..$#items) { my ($id,$temp) = @{$items[$i]}; my $kws = decode_json($temp); my $count = 0; foreach my $k (@kwlist) { if (defined($kws->{$k})) { $count++; } } }