Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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


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?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-10-24 21:09 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (247 votes). Check out past polls.