Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^4: Range Operator in If Statement

by Not_a_Number (Parson)
on Jan 06, 2012 at 11:15 UTC ( #946580=note: print w/replies, xml ) Need Help??

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

Could you explain...

Let me try. First,

[ 'A' .. 'F' ]

is a reference to an anonymous arrray (see references quick reference). To access, for example, the second element of this array, the syntax is:

[ 'A' .. 'F' ]->[1]

(You could also do ${ [ 'A' .. 'F' ] }[1] but I find the first form cleaner.)

Then, instead of hard-coding the array index, my snippet calculates it in the fashion indicated in LanX's post above. The only difference is that I've removed int() since perl happily does this part of the job for me when I use a floating point number as an array index.

To clarify, the following code, using a named array and an intermediate variable ($index), does the same thing:

my @array = ( 'A' .. 'F' ); my $ref_number = 42; # or whatever my $index = ( $ref_number - 1 ) / 8; my $bundle = $array[ $index ]; print $bundle;

hth, dave

Replies are listed 'Best First'.
Re^5: Range Operator in If Statement
by mmartin (Monk) on Jan 06, 2012 at 17:30 UTC
    Hey thanks for the explaination...

    I'll give it a try and see how it goes.

    Thanks Again,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946580]
erix just did a 12 km-run. Slow, but unstoppable!
[choroba]: My recent record is 10km in 1 hour :)
[perldigious]: Nice erix... perldigious' legs are so sore from portaging his kayak from place to place last weekend he can barely hobble 12m. :-)
[erix]: you're faster than me then :)
[erix]: well, at the moment, that is :)
[erix]: I should lose some weight although I keep telling myself that I'm doing extra training by lugging it along
[erix]: I saw a common kingfisher and a couple of bullfinches
[erix]: bullfinch ( a male and a female )

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-05-24 12:42 GMT
Find Nodes?
    Voting Booth?