Hi, I have an interesting regex puzzle I hope you might be able to help me with.
What I want to do is assign a 'score' to various strings, based on their prefix. So if a file starts with 'UKL' or 'USL', it receives a score of 10; if it starts with 'EUK' or 'EUS' it receives a score of 9, if 'CA2UK' 8...etc. The scores themselves are arbitrary, and there's no pattern as such to the prefixes. What I'd like to know is whether there's a better way to do this than my rather prosaic:
etc. Can all prefixes be combined into one regex, like m/^USL|UKL|EUK|EUS|CA2UK.../ and the score returned within that?
I should explain that the score is used to compare two strings, the one with the higher score being chosen over the lower-scoring. Maybe the whole idea of score-assignment is the wrong way to go about this?
Thanks for your time!
What I want to do is assign a 'score' to various strings, based on their prefix. So if a file starts with 'UKL' or 'USL', it receives a score of 10; if it starts with 'EUK' or 'EUS' it receives a score of 9, if 'CA2UK' 8...etc. The scores themselves are arbitrary, and there's no pattern as such to the prefixes. What I'd like to know is whether there's a better way to do this than my rather prosaic:
Code:
if ($string =~ m/^USL|UKL/) {
$score = 10;
} elsif ($string =~ m/^EUK|EUS/) {
$score = 9;
} elsif (....
etc. Can all prefixes be combined into one regex, like m/^USL|UKL|EUK|EUS|CA2UK.../ and the score returned within that?
I should explain that the score is used to compare two strings, the one with the higher score being chosen over the lower-scoring. Maybe the whole idea of score-assignment is the wrong way to go about this?
Thanks for your time!