Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: List into two-dimensional array

by ikegami (Patriarch)
on Dec 14, 2020 at 18:43 UTC ( [id://11125180]=note: print w/replies, xml ) Need Help??


in reply to List into two-dimensional array

You could keep using splice and achieve your goal by using a sub.

sub group { my $n = shift; my @rv; push @rv, [ splice(@_, 0, $n) ] while @_; return @rv; } my @rv = group($C, @list);

Update: Woops! Fixed issue brought up by haukex in reply.

Replies are listed 'Best First'.
Re^2: List into two-dimensional array
by haukex (Archbishop) on Dec 14, 2020 at 19:13 UTC
    sub group { my $n = shift; my @rv; push @rv, splice(@_, 0, $n) while @_; return @rv; }

    That just returns a copy of the original list. You probably meant push @rv, [ splice(@_, 0, $n) ] while @_;.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found