Solved

how to shuffle an array in perl

Posted on 2006-10-23
4
1,237 Views
Last Modified: 2008-09-07
Hi experts. I am trying to make a program with perl to shuffle an array. It is basically a card game. I am using pop, shift and push functions in order to do this but not too sure how. Help would be greatly appreciated thanks.
0
Comment
Question by:micromarch
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 17792294
perldoc -q shuffle
Found in pods/perlfaq4.pod
       How do I shuffle an array randomly?

       If you either have Perl 5.8.0 or later installed, or if you have
       Scalar-List-Utils 1.03 or later installed, you can say:

           use List::Util 'shuffle';

               @shuffled = shuffle(@list);

       If not, you can use a Fisher-Yates shuffle.

           sub fisher_yates_shuffle {
               my $deck = shift;  # $deck is a reference to an array
               my $i = @$deck;
               while ($i--) {
                   my $j = int rand ($i+1);
                   @$deck[$i,$j] = @$deck[$j,$i];
               }
           }

           # shuffle my mpeg collection
           #
           my @mpeg = <audio/*/*.mp3>;
           fisher_yates_shuffle( \@mpeg );    # randomize @mpeg in place
           print @mpeg;

       Note that the above implementation shuffles an array in place, unlike
       the List::Util::shuffle() which takes a list and returns a new shuffled
       list.

       You've probably seen shuffling algorithms that work using splice, ran-
       domly picking another element to swap the current element with

           srand;
           @new = ();
           @old = 1 .. 10;  # just a demo
           while (@old) {
               push(@new, splice(@old, rand @old, 1));
           }

       This is bad because splice is already O(N), and since you do it N
       times, you just invented a quadratic algorithm; that is, O(N**2).  This
       does not scale, although Perl is so efficient that you probably won't
       notice this until you have rather largish arrays.
0
 
LVL 2

Author Comment

by:micromarch
ID: 17792309
yeh that works but my co-workers want me to use push,shift and pop.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17792380
why would they want you to do that?
would your co-workers allow you to use an auxilliary array?
you could implement the slice using push and shift
0
 
LVL 2

Author Comment

by:micromarch
ID: 17792827
what do they mean by returning an element for example the pop function? i can return the element i removed somewheres else?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 105
Visual xHarbour 1 76
Enhanced For loop vs ForEach Loop 4 56
control image tags in a string ? 12 59
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now