Now, at what point, do you do the Fisher-Yates thing in your code.. when you've got a larger array to deal with I guess - but how large?

I'd suggest always using it. Using List::Util makes it simple, and List::Util is core as of perl 5.8.0. If, for some reason, you can't use List::Util, the algorithm is pretty simple, and is listed there in perlfaq4 for you.

Tried looking to see if List::Util uses a Fisher-Yates but it seems to be a .so binary, a library.

There is a C implementation, compiled into the library you mentioned, and a backup Perl implementation. You can read the Perl implementation by opening the file printed with perl -MList::Util -wle 'print $INC{"List/Util.pm"}' in your favorite editor. The C implementation requires viewing the perl source, shuffle is in ext/List/Util/Util.xs. Both are implementations of the Fisher-Yates algorithm.

As for feeling bad about splice, you should never feel bad about using tools. Generally, you should write your code first so that it is readable, maintainable, and possibly portable, then start worrying about efficiency, preferably after you've done some profiling.


In reply to Re: Re: Re: Re: Re: Choose random element of array? by Somni
in thread Choose random element of array? by McMahon

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.