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!
In reply to possible wierdness from "for @array" by hsinclai
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |