Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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
Replies are listed 'Best First'.
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 making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2015-07-28 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls