Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: About List::Util's pure Perl shuffle()

by blazar (Canon)
on Jul 12, 2007 at 09:58 UTC ( #626185=note: print w/ replies, xml ) Need Help??


in reply to Re^2: About List::Util's pure Perl shuffle()
in thread About List::Util's pure Perl shuffle()

Update: runrig pointed out that my shuffle was broken. I changed $a[$n] to $x=$a[$n] to fix it. It slowed down my solution, but not by much.

Ok, that's the same trick as BrowserUk's, except that in that form it's even more elegant. Anyway... c'mon! I'm not ashamed the very least to ask why does it work. I can't understand...


Comment on Re^3: About List::Util's pure Perl shuffle()
Select or Download Code
Re^4: About List::Util's pure Perl shuffle()
by ikegami (Pope) on Jul 12, 2007 at 13:29 UTC

    It's similar to print($i, $i++, $i);, at least in effect. Perl must place some form of pointer* to $a[$n]'s value on a stack instead of making a new scalar to save speed and memory. The result is that when $a[$n] is changed by the third expression in the list slice, it also changes the value of the second expression.

    By explicitly copying the value of $a[$n] into another variable ($x), changes in $a[$n] no longer affect the result of the slice.

    * — An alias? A pointer to the SV?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://626185]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-12-29 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls