anyone know if there is a way to use an array in a regular expression. suppose i want to find out if $sometext contains one of an array's elements. Obviously the code below doesn't work, but is there something similar that does work?
@array = qw/thing1 thing2 thing3/;
if ($sometext =~ /(@array)/) {
print "$1\n";
}
basically i've got a big list of words to search for, and i'd like to avoid something like this:
($sometext =~ /(thing1|thing2|thing3)/)
well your not really useing the array as the regx your useing a regex to find 1 element in your array.
you would end up doing it so the regex loops over the each element in an array.
check out perls map and grep functions.
here is a quick example.
my @nfiles=grep /my search pattern/, @files;
if you use the array @nfiles as your collection the values pulled from @files will populate the @nfiles.
if you use @nfiles as a scallar ie: $nfiles you will get the number of times you found a match vs. each match.
same things w/ map. except the map function can have a function to process your inbound data.
EDIT:
ok i think i know what your trying to do now.
and the answer is still yes.
populate your array then do a loop to itterate over each element of your array inserting it as the search pattern
something like
[code]
for (@words) {
if ( $sometext =~ /$/ ) { print "I FOUND IT BOB: $\n" );
}
excellent, the solution in your edit works great
thanks OP
you were already on the money i was supprised you didnt see it as you were typeing it out.