Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Range Operator in If Statement

by mmartin (Monk)
on Jan 04, 2012 at 20:55 UTC ( #946291=note: print w/ replies, xml ) Need Help??


in reply to Re: Range Operator in If Statement
in thread Range Operator in If Statement

Hey perlbotics, thanks for you QUICK reply...

Ohh ok I thought I could use the range operator for this type of thing, but guess not.

I wanted to use that because it seemed like it was the "shorthand" way of doing it.
Wanted a short way because I needed a whole bunch of those statements and I didn't want to have a ton of these statements:

if ($ref >= 1 && $ref <= 8) { ...... } if ($ref >= 9 && $ref <= 16) { ...... } if ($ref >= 17 && $ref <= 24) { ...... } if ($ref >= 25 && $ref <= 32) { ...... } #....more checks

But if that's what you recommend then I'll just use my original way I had like above and like you had...

Thanks again for the reply,
Matt


Comment on Re^2: Range Operator in If Statement
Download Code
Re^3: Range Operator in If Statement
by Marshall (Prior) on Jan 04, 2012 at 21:34 UTC
    This looks like a power of 2 problem. If you could give us some more detail about what you are doing, I think some very efficient solutions might be forthcoming.
Re^3: Range Operator in If Statement
by LanX (Canon) on Jan 05, 2012 at 01:56 UTC
    When classifying by intervals, better try $case = int( ($ref-1) / 8 ) , so you only have to test for the results 0,1,2,3...

    DB<104> for $ref (1,8,9,16,17,24,25,32) { print "$ref: ", int(($ref-1) +/8), "\n" } 1: 0 8: 0 9: 1 16: 1 17: 2 24: 2 25: 3 32: 3

    Cheers Rolf

Re^3: Range Operator in If Statement
by ambrus (Abbot) on Jan 05, 2012 at 11:43 UTC

    If you have multiple consecutive intervals, it's better to use an elsif cascade, such as

    if ($ref <= 8) { say "ref is at most 8."; } elsif ($ref <= 16) { say "ref is above 8 but at most 16." } elsif ($ref <= 24) { say "ref is above 16 but at most 24." } elsif ($ref <= 32) { say "ref is above 24 but at most 32." } else { sat "ref is above 32." }
    The elsif part means that if the previous condition was found true, the next one is not tested for, so eg. if $ref = 5 then the first branch is executed, the rest of the tests and branches are skipped.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2015-07-05 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls