Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by thezip (Vicar)
on Nov 27, 2013 at 00:31 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1064504]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-28 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found