Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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,

Replies are listed 'Best First'.
Re^3: Range Operator in If Statement
by Marshall (Abbot) 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 (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946291]
[shmem]: perldigious: as always - if it ain't broke, don't fix it. Ther must be a very compelling reason for changing column names in a database. Those are rare.
[Corion]: If you have whitespace in the column names in the database, whap the DBAs ;)
[shmem]: It's common for some vendors to have column names such as WRSTVG or some other such whizzbang, and another table where these names are mapped to something meaningful depending on how you look at the data
[shmem]: afair in SAP that occurs all the time

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2017-05-25 13:38 GMT
Find Nodes?
    Voting Booth?