I’m using this small snippet to determine whether or not a URL is currently being stored in an array:
if( $self->{_local} eq "true" && ! grep {m|^$new_href?$|} @m_href_array ) {
push( @m_href_array, $new_href );
push( @href_array, $new_href );
}
It seems to work but then my code throws an error saying:
Sequence (?$...) not implemented in regex; marked by <-- HERE in m/^javascript:SearchGo(?$ <-- HERE / at C:/Perl/site/lib/ACTC.pm line 152, <> line 1.
Can anyone explain why this is happening?
When searching for a string in an array, you can just use
eq, rather than a regular expression:However, if you really do need to use a regular expression (for example you are searching for a string matching a substring in the array, you should always quote the string, so that embedded special characters in your string do not have undesired effects:
Moreover, if all you care about is whether the value is there, somewhere, you can short-circuit as soon as you’ve found a match:
or
If you’re going to be doing a lot of searches, or your array is really long, you can make the process faster still by transforming the array into a hash, so you have O(1) lookups: