in reply to Array Fun

Then it randomly chooses a character in the array, adding that character to a new array all while replacing it with whitespace in the old as a nice way to filter out the chance of the random num coming back to the whitespace.
Well there's a different approach you can take for this, and more Perlish, too. And that is to just remove the character from the array with splice.

It has the advantage that you won't have a possibly slowdown near the end, when the array is almost only filled with spaces, to find that last non-space ones.

And here is how:

while ( @list ) { # list not empty $randint = int(rand(@list)); # pick a random index # remove the selected (one) item from @list, and append it to @lis +t2 push @list2, splice(@list, $randint, 1); }

Another cheaty way, is to look at shuffle in List::Util.

The latter uses the so-called Fisher-Yates shuffle, and if you look very closely, you'll find that both are entirely equivalent, only, Fisher-Yates reuses a (growing) part of the original array to hold the items for your @list2, as the room left for the original items in the same array (your @list) shrinks.

Replies are listed 'Best First'.
Re^2: Array Fun
by phantom20x (Acolyte) on Apr 06, 2007 at 18:00 UTC
    Interesting stuff! The module would have made it really easy. Gonna try to use the splice function a bit. Thanks for the feedback!