Home: Perl Programming Help: Regular Expressions:
Using OR matching



bernoulli
New User

Jun 2, 2008, 1:13 AM


Views: 6028
Using OR matching

Hi all,

I need to do something like:


Code
if ($code eq 'us' || $code eq 'au' || $code eq 'jp')


Would it be exactly the same if I do this?


Code
if ($code =~ '^(us|au|jp)$')


What would be a better way to write this sort of code (without using a hash) if I have 10 codes to match?

Regards,


KevinR
Veteran


Jun 2, 2008, 12:39 PM


Views: 6010
Re: [bernoulli] Using OR matching

There is nothing wrong with your first example, even if it is ten things to match, it will still be faster than a regexp. You could use an array:




Code
   

my @array = qw(us au jp ch ne de fr hw jn od);

foreach my $thing (@array) {

if ($code eq $thing) {

do something;

last;# breaks out of loop on the first match

}

}

-------------------------------------------------