in reply to Re: Making little array babies
in thread Making little array babies

To aid in my irrational goal to never use the C-style for loop in Perl -- as well as the more widely accepted goal of TMTOWTDI -- here's how I'd do that:

my @a = qw(a b c d e f g); my $n = 3; my @b; for (0 .. $#a/$n) { push @b, [ @a[$_*$n .. $_*$n+$n-1] ]; }

I was going to make the case that I like to avoid the C-style for loop because it looks ugly in Perl with all the sigils clogging things up, but after I typed out my alternative, I realized its readability is not much better. *shrug*

Replies are listed 'Best First'.
Re: Re: Re: Making little array babies
by jdporter (Paladin) on Apr 13, 2004 at 13:51 UTC

    Well, the direct equivalent* of mine, above, would be:
    (* other than scoping issues of $i)

    my $i = 0; while ( $i <= $#a ) { push @b, [ @a[ $i .. $i+$n-1 ] ]; } continue { $i += $n; }
    Thing I don't like about yours is the incessant multiplication by $n.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.