hsinclai has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I'm seeing wierd behavior in this snippet - and can't understand why..
Whatever number of elements @range contains, $#blah always contains half that number, minus 1.use strict; use warnings; my @lines = qx!cat /etc/services!; my @range = '1' .. $#lines; my @blah; push( @blah, $lines[ $#lines - splice( @range, int rand @range, 1 ) ] +) for @range; print $#blah . $/;
Replacing the qx!cat .. ! with open and getting the file contents into an array doesn't change the behavior... Replacing the "int rand" with a plain number, or POSIX floor/ceil doesn't change the behavior..
Using a more standard
seems to have no effect either..for ( @range ) { push( @blah, $lines[ $#lines - splice( @range, int rand @range, 1 ) + ] ) }
Can anyone see why this happens? (my /etc/services file has 9252 lines, same effect with other files)
Is it a bad idea to put code in between the brackets of $array[indices] ?
Many thanks for your insight!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: possible wierdness from "for @array"
by moritz (Cardinal) on Mar 31, 2009 at 16:53 UTC | |
by hsinclai (Deacon) on Mar 31, 2009 at 17:18 UTC | |
Re: possible wierdness from "for @array"
by ikegami (Patriarch) on Mar 31, 2009 at 16:56 UTC | |
Re: possible wierdness from "for @array"
by CountZero (Bishop) on Mar 31, 2009 at 20:29 UTC |