Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Array One-liners

by japhy (Canon)
on Sep 24, 2000 at 15:55 UTC ( #33835=snippet: print w/ replies, xml ) Need Help??

Description: Less than 80 characters each! Whoo-hoo!
# randomize, via Fisher-Yates
@array[-$i,$j] = @array[$j,-$i] while $j = rand(@array - $i), ++$i < @
+array;

# remove a chunk from the end and place at the beginning ("cutting")
splice @array, 0, 0, splice(@array, rand @array);

# weave an array, method 1 (ABCDEF => ADBECF)
splice @array, @array/2 - $_, 0, pop @array for 0 .. (@array/2)-1;

# weave an array, method 2 (ABCDEF => DAEBFC)
splice @array, @array/2 + $_, 0, shift @array for 0 .. (@array/2)-1;
Comment on Array One-liners
Download Code
RE: Array One-liners
by merlyn (Sage) on Sep 24, 2000 at 18:44 UTC
    splice @array, 0, 0, splice(@array, rand @array);
    Good stuff, but typically we spell that:
    unshift @array, splice @array, rand @array;
    splice is cool, but you don't need to use it for the common thingies.

    -- Randal L. Schwartz, Perl hacker

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-07-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls