in reply to Arrays manipulation

obvious and relativly fast - an array of arrays - could do the foreach as a map as well, but that is not as clear.
#!/usr/bin/perl -w use strict; use Data::Dumper; my @array = ('nfs,7,rw', 'afp,12,rro', 'cifs,32,ro', 'dns,5,rw', ); my $cols = []; foreach my $row (0..$#array) { my @cols = split /,/, $array[$row]; map {$cols->[$_]->[$row] = $cols[$_]} (0..$#cols); } print Dumper($cols);

output is
$VAR1 = [ [ 'nfs', 'afp', 'cifs', 'dns' ], [ '7', '12', '32', '5' ], [ 'rw', 'rro', 'ro', 'rw' ] ];

Replies are listed 'Best First'.
Re: Re: Arrays manipulation
by sauoq (Abbot) on May 01, 2003 at 09:16 UTC

    Easier to read and avoids the map in void context:

    #!/usr/bin/perl my @array = ( 'nfs,7,rw', 'afp,12,rro', 'cifs,32,ro', 'dns,5,rw', ); my @cols; for my $row (@array) { my $i = 0; push @{$cols[$i++]}, $_ for split /,/, $row; } use Data::Dumper; print Dumper(\@cols);

    -sauoq
    "My two cents aren't worth a dime.";
    
      And without an explicit counter variable:
      my @cols; for my $row (@array) { my @f = split /,/, $row; push @{$cols[$_]}, $f[$_] for 0 .. $#f; }

      Makeshifts last the longest.

        Six of one, half a dozen of the other. You made the array explicit, I made the counter explicit. Personally, I prefer for split /,/, $row because it keeps the meat all on one easy-to-eat line. I'm also comfortable enough with C to be happy with the autoincrementing counter.

        Didja benchmark them? (Kidding, of course.)

        -sauoq
        "My two cents aren't worth a dime.";