http://www.perlmonks.org?node_id=1064450


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!