>>>>> "DT" == Dave Tang <d.t...@imb.uq.edu.au> writes:

  DT> On Tue, 03 Nov 2009 17:48:55 +1000, Thomas Bätzler
  DT> <t.baetz...@bringe.com>  wrote:

  >> Dave Tang <d.t...@imb.uq.edu.au> asked:
  >>> Just a quick question, how does Perl interpret something like
  >>> $array[0.7995038473872]?
  >> 
  >> Just like $array[ int(0.7995038473872) ], i.e. the floating point
  >> number  is coerced into an integer value by cutting off the decimal
  >> places.

  DT> Ah cool. So when using rand in this aspect (i.e. getting random
  DT> indexes  from an array), the int is negligible?

if you think about it, array indexing needs an integer so the index
expression must be coerced to one. this allows for the common idiom of
random selection from an array with $array[rand @array]. note that rand
never returns the value of its argument, just numbers from 0 to below
it. so that will never return the count of the array which would be 1
past the last index. so it works very well in this context. in fact you
can say the array index provides an integer context in the same way you
have scalar and list contexts and hash keys are provided a string
context.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to