You can also assign in 2d directly :-
my $array2d = []; my @list = (split /,/,join (",",@array)); $array2d->[$_%3][int($_/3)]=$list[$_] for (0..$#list);
or even
my @array2d = (); my $c=0; $array2d[$c%3][int($c++/3)]=$_ for (split /,/, join (",",@array));
And let us compare with :-
#!/usr/bin/perl -w use strict; use Devel::Timer; my $runcount=500; my $t = new Devel::Timer(); my @array = ('nfs,7,rw', 'afp,12,rro', 'cifs,32,ro', 'dns,5,rw', ); $t->mark('V1'); for my $runs (1..$runcount) { my $cols = []; foreach my $row (0..$#array) { my @cols = split /,/, $array[$row]; map {$cols->[$_]->[$row] = $cols[$_]} (0..$#cols); } } $t->mark('V2'); for my $runs (1..$runcount) { my @splitted_up = (); my $cnt = 0; push @{ $splitted_up[($cnt ++) % 3] }, $_ foreach (split (/,/, join (",",@array))); } $t->mark('V3'); for my $runs (1..$runcount) { my $array2d = []; my @list = map {split /,/,$_}(@array); $array2d->[$_%3][int($_/3)]=$list[$_] for (0..$#list); } $t->mark('V4'); for my $runs (1..$runcount) { my @array2d = (); my $c=0; $array2d[$c%3][int($c++/3)]=$_ for (split /,/, join (",",@array)); } $t->mark('V4 end'); $t->report();

And the Winner is :-

Devel::Timer Report -- Total time: 0.1663 secs Interval Time Percent ---------------------------------------------- 01 -> 02 0.0530 31.87% V1 -> V2 03 -> 04 0.0443 26.65% V3 -> V4 04 -> 05 0.0370 22.24% V4 -> V4 end 02 -> 03 0.0319 19.15% V2 -> V3 00 -> 01 0.0002 0.09% INIT -> V1
The second suggested code posted performed best for my given hardward config, but always try speed tests on the correct hardware as OS's etc can effect performance.
push @{ $splitted_up[($cnt ++) % 3] }, $_ foreach (split (/,/, join (",",@array)));
Hope this helps
UnderMine

In reply to Re: Arrays manipulation by UnderMine
in thread Arrays manipulation by hotshot

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.