in reply to Re^2: Regex with variables
in thread Regex with variables

Hi Stevieb,

I wasn't considering performance but rather the ease of maintenance of a list of items. The OP has $targets (plural) as the name of his string, so I assumed that at some point he had a list, and then put it into a string.

I agree that if he originally had a string like that (e.g. from a log file) then a regexp would be the way to find a substring of it.

But if he has a list then putting it into a string and looking for a match is headed in the wrong direction, IMO.

Update: I wanted to know, so now I do. In this case join()ing the list into a string and then using a regexp is still much faster than populating a hash:

use Benchmark qw(:all); cmpthese ( 5000000, { 'string' => 'string()', 'hash' => 'hash()', 'list2string' => 'list2string()', }); # other subs as before sub list2string { my $str = join( ' ', qw/1689 9679 2792 2514 5472 1520 9342 5544 1268 + 0165 1979 7314 2101 7221 9539 3882 1812/ ); my $test = 2101; if ($str =~ $test){ # do stuff } } __END__ Rate hash list2string st +ring hash 62406/s -- -88% +-95% list2string 530223/s 750% -- +-61% string 1355014/s 2071% 156% + --

But I still wouldn't do it myself as combining discrete values into a string just feels wrong.

Replies are listed 'Best First'.
Re^4: Regex with variables
by stevieb (Canon) on Jun 29, 2015 at 15:21 UTC

    If the OP's data was indeed in a list, I'd probably opt for grep(), if grep(/$test/, @list).... The good thing about Perl, is TIMTOWTDI ;)