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:

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.


In reply to Re: Array Fun by bart
in thread Array Fun by phantom20x

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.