Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by thezip (Vicar)
on Nov 27, 2013 at 00:31 UTC ( #1064504=note: print w/replies, xml ) Need Help??


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

dvinay,

I could't help but notice that your array just screams to be an array of arrays. I say this because it regularly repeats itself every three elements. I know this wasn't your original spec, but I offer the following advice.

If you structured your array as:

my @array = qw/ create mount remove /; my $array = [ ['create', 'mount', 'remove'], ['create', 'mount', 'remove'], \@array, # essentially the same values \@array, ... ];

... then you could easily add items to the end of each of sub-array by iterating and pushing:

#!/perl/bin/perl use strict; use warnings; use Data::Dumper; for my $subarray (@$array) { push(@$subarray, 'foo'); } print Dumper($array);
__OUTPUT__ $VAR1 = [ [ 'create', 'mount', 'remove', 'foo' ], [ 'create', 'mount', 'remove', 'foo' ], [ 'create', 'mount', 'remove', 'foo' ], [ 'create', 'mount', 'remove', 'foo' ], ];

What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2019-10-23 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?