[Pdx-pm] accessing list elements

Joshua Keroes jkeroes at eli.net
Wed Sep 3 18:07:46 CDT 2003


On (Wed, Sep 03 15:45), Michael Rasmussen wrote:
> When reading through some code I came across this:
> 
> my $pic = $files[rand @files];
> 
> huh?  rand returns a "random fractional number". And lists are
> accessed through integers.  
> 
> Or not?
> 
> A simple test case shows that you can access list members with
> fractional numbers.  This is handy, but puzzling behavior.
> I couldn't find a reference to this in the docs.
> 
> Is there any explanation to this besides "Perl does what you expect"?

perl -le '@a=qw/a b c d/; print @a[0.923423,1.23432,2.345,3.87543]'
perl -le '@a=qw/a b c d/; for (0..9) { $n = rand(3); print "$n: $a[$n]" }'

Apparently, the array lookup implicitly converts the float to an int. Handy!

J



More information about the Pdx-pm-list mailing list