in reply to Array shuffle code produces 'Modification of non-creatable array value attempted' error

The error occurs when you assign to a negative index so large that it indicates a negative element.
$ perl -e' @a = qw( abc def ); $a[-1] = "ghi"; $a[-2] = "jkl"; $a[-3] = "mno"; ' Modification of non-creatable array value attempted, subscript -3 at - +e line 4.

In your code, that will happen when an empty array is passed to shuffle. Fix by changing

while ( --$i )
to
while ( --$i > 0 )
  • Comment on Re: Array shuffle code produces 'Modification of non-creatable array value attempted' error
  • Select or Download Code