in reply to Re: How it works?
in thread How it works?
"rand @_ is like rand 15, which returns an integer between 0 and 14."
Actually, that's incorrect (with respect to rand returning an integer). The first sentence of the rand documentation reads:
"Returns a random fractional number greater than or equal to 0 and less than the value of EXPR."
As an example (which I'll continue to use below):
$ perl -le '$x = rand 2; print $x' 0.0874574767967786
I believe, although I can't find any documentation to back it up, that Perl knows the index must be an integer and applies a behind-the-scenes int (or equivalent) to the index. I can use the above rand result directly without getting a warning:
$ perl -wle '@y = qw{a b c}; print $y[0.0874574767967786]' a
Even B::Deparse only reports 0 as the index:
$ perl -MO=Deparse -e '@y = qw{a b c}; print $y[0.0874574767967786]' @y = ('a', 'b', 'c'); print $y[0]; -e syntax OK
I'd be interested if anyone has more information about this (including, but not limited to, if this behaviour is documented).
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How it works?
by AnomalousMonk (Archbishop) on Jan 21, 2016 at 20:25 UTC | |
by kcott (Archbishop) on Jan 22, 2016 at 06:43 UTC |