Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Adding an new element after every 5th element in array

by Kenosis (Priest)
on Nov 26, 2013 at 19:23 UTC ( #1064450=note: print w/replies, xml ) Need Help??


in reply to Adding an new element after every 5th element in array

Since the commands are in a regular pattern, you could do this:

use strict; use warnings; use Data::Dumper; my @array = ( "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove", "create", "mount", "remove" ); my @newArray; while ( my @tmp = splice @array, 0, 3 ) { push @newArray, @tmp, 'newelement'; } print Dumper \@newArray

Partial output:

$VAR1 = [ 'create', 'mount', 'remove', 'newelement', 'create', 'mount', 'remove', 'newelement', 'create', 'mount', 'remove', 'newelement', ...

Hope this helps!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1064450]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-12-16 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (448 votes). Check out past polls.

    Notices?