On Tuesday, April 23, 2002, at 03:05 PM, Jonathan E. Paton wrote:
>>> Is there a better way to simplify the syntax when testing for multiple >>> conditions? >> >> Your original code is the proper way to do it. All the other solutions >> proffered are inferior, IMO. > > Use 'or' and 'and' to remove clutter, they have different precedance and > are often useful in this context. E.g. > > if ($string eq "First" or $string eq "Second" or $string eq "Third") { > ... > } > >> The suggestion of: >> >> if($something =~ /^(string0|string1|string2)$/) >> >> is not equivalent to your original condition. It's also likely to be >> inefficient. > > I get 1 min 15.983sec for "normal" three condition if, and 52.305sse for > a regex approach. My > test code is: > > my $type = shift; > my $iterate = 1000000; > > my $item = "lock"; > > if ($type eq "normal") { > for (0..$iterate) { > print " " if $item =~ /^(?:apple|banana|lock)$/; > } > } are you messing with our minds on porpoise? looks like this is the regex case > elsif ($type eq "regex") { > for (0..$iterate) { > print " " if $item eq "apple" or $item eq "banana" or $item eq > "lock"; > } > } and this is 'normal' so, was your benchmark right, or switched? > __END__ > > If in doubt, benchmark. It can produce non-intutive results! Guess I > should have bothered to use > Benchmark.pm The regex approach is common, and is often much clearer... > being read as: > > "print something if item matches apple or banana or lock" > > Jonthan Paton > > __________________________________________________ > Do You Yahoo!? > Everything you'll ever need on one web page > from News and Sport to Email and Music Charts > http://uk.my.yahoo.com > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]