if ($ref =~ m/a/) {
$type = "Type A";
} elsif ($ref =~ m/b/) {
$type = "Type B";
} elsif ($ref =~ m/c/) {
$type = "Type C";
} elsif ($ref =~ m/d/) {
$type = "Type D";
}
My problem is I have some referers that will match to b, but really need to be classified as some other Type further down the if block. I tried to use the next statement but that didn't work.
elsif ($ref =~ m/b/) {
if ($ref =~ m/bc/) {
next;
}
$type = "Type B";
}
Then I tried
elsif ($ref =~ m/b/ && $ref !~ m/bc/) {
$type = "Type B";
}
and that skips the b block correctly, but I have something like bcs that needs to be Type C and bcb that needs to be Type B. So I really need to get something similar to what I was trying with the next statement. Any suggestions?
elsif ($ref =~ m/xcode=b([^&]+)/ && $ref !~ m/xcode=bc[snmtl][^&]+/) {
$type = "Type B";
}
This results in false positives when the URL has file.html?xcode=b&ycode=2&xcode=bcs&zcode=return In this case the first xcode=b supercedes the second xcode=bcs, but I have no guarantee of what order they will be in. I'd like to avoid creating a list of all valid "b" values but that's starting to look like the simplest solution.
I don't completly understand how you want to build a general rule _without_ letting your script know all the valid values for different types...
My problem is I have some referers that will match to b, but really need to be classified as some other Type further down the if block.
Note that
1. Presuming you're going through a file (log) in a while loop, next will skip to the next line and ignore any matching on this line completely. So that won't work.
2. if/elsif can catch you in exactly these circumstances. Since you catch the first condition in an elsif, it's gong to ignore anything after. Right?
So, maybe a simple workaround is to not use elsif at all, just a series of if's. You have to use caution when you do this, as later if's will overwrite previous ones, which sounds like what you want to do.
$string = 'aaaabccc';
if ($string =~ /a/) { $letter = 'A'; }
if ($string =~ /b/) { $letter = 'B'; }
if ($string =~ /c/) { $letter = 'C'; }
print $letter;
So at first, $letter is A, but it gets overwritten and becomes B, then in the third match becomes C. You may want more complex if's further down:
if ($string =~ /a/) { $letter = 'A'; }
if ($string =~ /b/) {
$letter = ($string =~ /a/)?'both A and B':'B';
}
if ($string =~ /c/) {
$letter = ($string =~ /[ab]/)?'Contains A or B, and C':'C';
}
As always, TMTOWTDI in Perl. :-)