It really depends on what you intend to do with the data later. I'll assume that it's not looping over the whole collection, since the original array is the best way to do that.

So. On reflection, if you want a set number of groups, then your modulus is as neat a way as any - sorry about that - but it does need to be fixed for the % 0 problem. This one's a bit closer to the format of your original:

# we have @files my $number_of_files = scalar(@files); my $number_of_groups = 10; my @groups; for (1..$number_of_files) { my $modvalue = $number_of_groups % $_; push @{$groups[$modvalue]},$files[$_ - 1]); }

but there's always another way. The extra braces just make sure that the $counter goes out of scope when it's no longer needed:

{ my $counter = 0; my $number_of_groups = 10; for (@files) { push(@{$groups[$counter++]},$_); $counter = 0 if ($counter == $number_of_groups); } }

or if you wanted groups of a fixed size:

@{groups[$counter++]} = splice(@files,0,$group_size) while @files;

or maybe 26(ish) groups:

my %groups; push (@{$groups{substr($_,0,1)}},$_) for @files;

or perhaps it's more useful to group by suffix?

for (@files) { my $suffix = (m/\.(\w+)$/) ? $1 : 'none'; push(@{$groups{$suffix}},$_); }

The last two versions use a hash instead of an array to hold the final collection of groups. The advantage of this is that you can retrieve selected parts directly, without having to scan through the collection again. In the last case, for example, $groups{'jpg'} holds an array of all the files with the suffix .jpg: to get at it you just use

for (@{$groups{'jpg'}}) { ... }

Which is only going to be useful if you ever want to present a list of files of a certain type, but in general, if you're going to do all this work i'd say you might as well store the information in a way that adds value by capturing some useful regularity in the collection.

Incidentally, it just struck me that your objection to the original array was that it took a long time to loop over it. But in order to subdivide it you probably need to loop. Er. Perhaps a better approach is needed earlier on, when the list is first assembled?


In reply to Re^3: two-dimensional arrays by thpfft
in thread two-dimensional arrays by Ras

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.