CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



bernoulli
New User

Jun 2, 2008, 1:13 AM

Post #1 of 2 (1949 views)
Using OR matching Can't Post

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

Post #2 of 2 (1931 views)
Re: [bernoulli] Using OR matching [In reply to] Can't Post

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

}

}

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives