Solved

how to shuffle an array in perl

Posted on 2006-10-23
4
1,267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Rubik's Cube Code for Effective Presentation 3 74
Advice on Xojo as a development tool over VB. 4 81
ASP/VB email question 4 62
youtube blocking politics 4 80
In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

749 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