Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Another variation that's useful if the elements of the array itself are large and therefore costly to shuffle or perhaps more importantly, if its necessary to keep the array in it's original order.

Instead of shuffling the array, create and shuffle an array of indexes, then subset it to produce a slice from the real array.

#! perl -sw use strict; sub shufl (\@) { my $r=pop; $a = $_ + rand @{$r} - $_ and @$r[$_, $a] += @$r[$a, $_] for (0..$#{$r}); } my @array = qw(the quick brown fox jumps over the lazy dog and went st +raight into a puddle); my @indexes = (0..$#array); shufl(@indexes); my ($comp1, $comp2, $comp3, $comp4, $comp5) = @array[@indexes[0..4]]; print"$comp1, $comp2, $comp3, $comp4, $comp5\n"; __END__ #Ouput C:\test> jumps, dog, a, the, fox C:\test> a, the, lazy, brown, into C:\test> and, quick, a, puddle, fox C:\test> a, and, the, the, into C:\test> dog, jumps, went, the, straight C:\test> lazy, quick, and, into, fox C:\test> the, went, the, brown, over C:\test>

Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy

In reply to Re: random elements from array - new twist by BrowserUk
in thread random elements from array - new twist by felwick

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2020-10-31 23:23 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (291 votes). Check out past polls.