Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why do I need to shuffle an array by reference?

by ajdelore (Pilgrim)
on Aug 13, 2003 at 22:58 UTC ( [id://283712]=perlquestion: print w/replies, xml ) Need Help??

ajdelore has asked for the wisdom of the Perl Monks concerning the following question:

I am using the good old fisher-yates shuffle from perlfaq4:

my $ref = \@list; for (my $i = @$ref; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$ref[$i,$j] = @$ref[$j,$i]; } # @list is modified in place

Question: why do I have to use a reference? I can't figure this out. I tried:

for (my $i = scalar(@list); --$i; ) { my $j = int rand ($i+1); next if $i == $j; @list[$i,$j] = @list[$j,$i]; }

This didn't work... Some elements were duplicated in the changed list. This is probably just a lack of understanding references well, but could some monk please explain this?

</ajdelore>

Replies are listed 'Best First'.
Re: Why do I need to shuffle an array by reference?
by revdiablo (Prior) on Aug 13, 2003 at 23:41 UTC

    The only reason the example code uses an array reference is because it was intended to be a subroutine, and it edits the array in-place. If it were passed a simple list, there would be no way for it to "know" what array to edit. In short, the algorithm itself does not need a reference -- it's the subroutine that does.

Re: Why do I need to shuffle an array by reference?
by Zaxo (Archbishop) on Aug 13, 2003 at 23:24 UTC

    I don't see a thing wrong with your second shuffle. It works for me. Are you sure your testing was correct and applied to the arrays you thought?

    There is no reason but, perhaps, economy, that an array reference is used there.

    After Compline,
    Zaxo

Re: Why do I need to shuffle an array by reference?
by BrowserUk (Patriarch) on Aug 13, 2003 at 23:47 UTC

    My guess would be that it is common practice to code the shuffle as a sub so that it can be called rather than replicated in-place. Once you place the routine in a sub, it makes sense to pass the array by reference, as to not do so would mean passing the contents of the whole array as a list, which would become prohibitively expensive for large arrays.

    For reference, if you are using the shuffle rather than just coding for the sake of learning, there is an implementation of it available in List::Util which comes as standard if you have 5.8.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Why do I need to shuffle an array by reference?
by Abigail-II (Bishop) on Aug 14, 2003 at 07:42 UTC
    That's an old version of the FAQ you have. The silly line next if $i == $j; isn't included in modern versions of the FAQ.

    And as others have already indicated, you don't need references. Could you have an example @list that fails for you?

    Abigail

Re: Why do I need to shuffle an array by reference?
by ajdelore (Pilgrim) on Aug 14, 2003 at 16:33 UTC

    Oops... You are right. The code I posted works. <wipes egg off face>

    When I was having problems, I was getting something that I suspected was an off-by-one error, as elements of the list were being duplicated.

    Likely, when I changed it from directly referencing the list back to using a reference, I fixed some sort of typo or error that I was making elsewhere.

    Thanks to all for the feedback, and also ++Abigail-II for pointing out next is unneccessary.

    </ajdelore>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://283712]
Approved by Kanji
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-26 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found