I want to match a string I read against an array of possible matches. Would also be good if it could return the index of the match string. I COULD hard code easily enough … and probably will for expediency this time around, but for general case, I’d like to see if this is possible. I’ve looked through some books and online (including stackoverflow), but can’t find what I’m looking for and can’t quite connect the dots to figure it out myself.
Here’s an example of the general kind of thing I’m looking for … of course it does not work, which is why I am asking for help. But I hope it is sufficient to infer my intent.
Example:
my $patterns;
my $line;
my $c = 0 ;
$patterns{$c++} = "$exact" ; # where the $ in $exact marks the beginning of line.
$patterns{$c++} = "$T?:" ; # where the ? is the normal wildcard
$patterns{$c++} = "" ;
$patterns{$c++} = "exact" ;
open (FILE, "example.txt") || die "Unable to open file.\n";
while (my $line = <IN>) {
my $idx = -1;
for ($i=0; $i<$c :$i++) {
if ($line =~ /$patterns{$i}/ ) { $idx = $i ; }
}
$result = $idx; # of course this will return index of last pattern match, but that's ok
}
close(FILE);
Without knowing exactly what you are looking for, this is your code transformed into actual Perl code.