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: Beginner:
is there any easier way to do this?

 



menchen
User

Aug 21, 2000, 6:21 AM

Post #1 of 2 (903 views)
is there any easier way to do this? Can't Post

if (($var == 4) | | ($var == 13) | | ($var == 23) | | ($var == 54)) {
do something;
}

note: numbers are not related at all.
or you can see it this way:

if (($var eq "string1") | | ($var eq "string2") | | ($var eq "string3") | | ($var eq "string4") | | ($var eq "string5") | | ($var eq "string6")) { do something}

jason


rGeoffrey
User / Moderator

Aug 21, 2000, 7:38 AM

Post #2 of 2 (903 views)
Re: is there any easier way to do this? [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


my %valid_conditions = map { $_ => 1 } ("string1", "string2", "string3", "string4", "string5", "string6");

foreach my $string ('string1', 'string3', 'garbage') {
if (exists $valid_conditions{$string}) {
print "string '$string' is valid\n";
} else {
print "string '$string' is NOT valid\n";
}
}
</pre><HR></BLOCKQUOTE>

You can use a hash that knows all the valid strings. If the string is valid, then it is a key in the hash with a value of 1, but we never actually look at the value in this example.


 
 


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

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