Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 musing on the Monastery: (7)
As of 2016-12-08 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (143 votes). Check out past polls.