Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Making little array babies

by davido (Cardinal)
on Apr 12, 2004 at 04:58 UTC ( #344329=note: print w/replies, xml ) Need Help??

in reply to Making little array babies

Here's one (untested) way...

my @images = qw/IMAGE1 IMAGE2 IMAGE3 IMAGE4 IMAGE5 IMAGE6 IMAGE7 IMAGE8 IMAGE9 IMAGE10 IMAGE11/; my @lol; while ( @images ) { push @lol, [ map { (@images) ? shift @images : undef } 1..3 ]; }

An artifact of this method is that if your initial array has a number of elements not evenly divisible by three you'll get one or two final elements that equate to undef in the last element in @lol.


Replies are listed 'Best First'.
Re: Re: Making little array babies
by nmcfarl (Pilgrim) on Apr 12, 2004 at 11:18 UTC

    I personally like davido's solution the best, if you can live with the undef items at the end. However I would change the push, in the case where the baby arrays are small to :

    push @lol, [ shift(@images), shift(@images), shift(@images) ];

    It's just more explicit. The guy that comes after you won't have to think about what is going on here, it's obvious.

    Of course if the actual baby arrays are large, or thier length changes programatically, stick with his original code.

      Thank you all for the help,

      It was a good introduction to understanding how to store arrays in an array. I ended up using a combination of davido and nmcfarl's code which worked quite nicely and was understandable to look at quickly.

      Also, as I was halfway through reading the responses, I noticed someone named Randal L. Schwartz had replied, then a few minutes later I happen to glance down at the author of the book open in front of me... quite an honor.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-03-30 02:02 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (73 votes). Check out past polls.