Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: IF condition with a range

by tobyink (Canon)
on Jul 17, 2018 at 08:42 UTC ( [id://1218633]=note: print w/replies, xml ) Need Help??


in reply to Re^2: IF condition with a range
in thread IF condition with a range

You're thinking of something like this:

if ( it is equal ) { do something; } elsif ( it is equal or close ) { do something else; }

But you want:

if ( it is equal ) { do something; } if ( it is equal or close ) { do something else; }

Or even:

if ( it is equal or close ) { if ( it is equal ) { do something; } do something else; }

Replies are listed 'Best First'.
Re^4: IF condition with a range
by Anonymous Monk on Jul 17, 2018 at 08:44 UTC
    Aha, so you mean I should have only ifs and not if-elseif. Good, I will try that!
      Aha, so you mean I should have only ifs and not if-elseif.

      Note that there is a potential pitfall of if (...) {...} if (...) {...} instead of if (...) { if (...) { ... } ... } - it's easy to make a mistake with the logic:

      if ( /^[brc]at$/ ) { # match "bat", "rat", or "cat" print "It's an animal\n"; if ( /^b/ ) { print "... and it flies!\n"; } }

      This is not the same as:

      if ( /^[brc]at$/ ) { print "It's an animal\n"; } if ( /^b/ ) { print "... and it flies!\n"; }

      Because the latter will match any word that starts with b.

      Of course, in this case, it seems obvious that if the numbers are equal, they will also be within +/- 1 of each other, but mistakes like the above can happen if you don't think the logic through (and test it!).

      Update: For example, consider the special case of Inf that syphilis suggested, which I've now added as a test case to my other post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218633]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found