Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re: Making little array babies

by revdiablo (Prior)
on Apr 12, 2004 at 21:50 UTC ( [id://344544]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found