You could take advantage of hashes
my %hash; push @{$hash{$_}}, $_ for @elements; foreach (keys %hash) { # gets passed an array reference, deref if you so desire do_something_with($hash{$_}) if scalar @{$hash{$_}} > 0; }
Looks like you're dirtying your arguments there, maybe you want to have it return modified versions of the arguments e.g
my @data; foreach (keys %hash) { push @data, do_something_with($hash{$_}) if scalar @{$hash{$_}} > 0; }
Or maybe you have a perfectly valid reason for doing what you're doing like logging the sets to a file.

Better yet I could actually answer your question ...

my @elems = qw(foo bar baz quux ichi ni san shi go roku hachi); my $set = 3; while(scalar @elems) { do_something_with(splice(@elems, 0, $set)); }
This however has the unfortunate side of effect of erasing the contents of the array, but does the job.
HTH

_________
broquaint

update: the penny dropped ...


In reply to Re: Traversing succeding sets of an array by broquaint
in thread Traversing succeding sets of an array by very empty

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.