Solved

how to shuffle an array in perl

Posted on 2006-10-23
4
1,256 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
This is about my first experience with programming Arduino.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

821 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