Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
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!


Comment on Re: Adding an new element after every 5th element in array
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-04-17 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls