I have a list of rows in MySQL that look like:
colA | colB
^csdude | 20200114
robzil+a | 20210811
^phran(k|que)$ | 20190420
co+pster | 20200901
Now I'm running a regex to see if my variable matches any of them, and if so then I want to return colB.
I'm currently doing it in a loop, like:
# use selectall_array to return rows as @arr
foreach $key (@arr);
($colA, $colB) = @$key;
if ($str =~ /$colA/) {
$filter = $colB;
}
}
As my list of rows grows, though, that's a slow way to do it. I recognize that it could be a lot faster to process with:
# wrap it in ( ) so I can get $1
$pattern = '(';
foreach $key (@arr);
($colA, $colB) = @$key;
$pattern .= $colA . '|';
}
$pattern =~ /|$/)/;
# this returns the matched pattern
if ($str =~ /$pattern/) { return $1; }
But how do I return the code in $colB?
I tried creating a second hash with the colA regex code as the key, but the $1 match returns the text that matched instead of the regex code:
# wrap it in ( ) so I can get $1
$pattern = '(';
foreach $key (@arr);
($colA, $colB) = @$key;
$match{$colA} = $colB;
$pattern .= $colA . '|';
}
$pattern =~ /\|$/)/;
# assume $str = 'csdude55';
# $1 equals "csdude" instead of "^csdude"
if ($str =~ /$pattern/) {
return $match{$1};
}