Solved

how to shuffle an array in perl

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Simple Linear Regression
Six Sigma Control Plans

695 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