http://www.perlmonks.org?node_id=474445
Category: Utility Scripts
Author/Contact Info Adrien 'Axioplase::Pied' /msg me
Description: I sometimes miss Ocaml while parsing lists... Yesterday, I needed something that would take a list of tuples and give me back the list of all the nth elements of each tuple. Here we go! Update: changed the name, as graff showed out it was just a kind of matrix transposer in fact :)

use strict;
##################
#The Function
##################
sub demux {
  my ($rsrc,$separator)=@_; #a reference to the list of tuples, and th
+e separator
  my @res; # result
  my $nbelem=@{[split /$separator/, $$rsrc[0]]}; #this may be a little
+ bit *gruik* to get the number of elements in each tuple...
  foreach my $n (0.. $nbelem-1){
    push @res , [(map { (split /$separator/ )[$n] } @{$rsrc} )]; #that
+ took me ages to figure out :D
  }
  return @res; #guess what!
}

###################
#The Test
###################

sub even { #self explanatory
  return $_ if ($_=int(rand(100)),$_%2==0);
  return $_+1;
}
sub odd { #worse :D
  return &even+1;
}

my @liste;
push @liste, (&odd.",".&even.",".&odd.",".&even.",".&odd.",".&even) fo
+reach(1..10); #create a list of even and odd numbers alternating (so 
+that we understand easily)
print "$_\n" foreach (@liste); #print it out
print "\n";

my @re= demux(\@liste,","); # it's a kind of magic

print "@{$_}\n" foreach(@re); # Yehaaaaww!!
Let's run it:
C:\Documents and Settings\xxxx\My Documents\perl>"demux.pl"
85,34,15,38,53,80
73,38,31,92,17,96
89,10,31,96,35,60
45,68,23,90,33,12
99,100,27,52,25,28
85,50,75,28,73,4
45,8,45,48,55,66
57,10,5,4,43,20
61,36,1,22,7,74
37,16,71,32,81,42

85 73 89 45 99 85 45 57 61 37
34 38 10 68 100 50 8 10 36 16
15 31 31 23 27 75 45 5 1 71
38 92 96 90 52 28 48 4 22 32
53 17 35 33 25 73 55 43 7 81
80 96 60 12 28 4 66 20 74 42

looks like it worked.


Now, the explanation, for those who are as good as I am in Perl, which means that they *may* need to read this or use perldoc for a few hours before understanding the code ^^
(yes, I'm an eternal newbie)

my ($rsrc,$separator)=@_;
first argument is a reference to the tuples list second is the separator of each element of the tuple.

my @res;
will be a list of list references.

my $nbelem=@{[split /$separator/, $$rsrc[0]]};
I want to know how many elements are in each tuple. I had to use an anonymous array, because perl complained when I had
$nbelem=split/$separator/,$$rsrc[0]
that using "@_" implicitely was deprecated.
Since $rsrc is a ref to an array, $$rsrc[0] is that array's first element (here, a tuple)

push @res , [(map { (split /$separator/ )[$n] } @{$rsrc} )];
split /$separator/
Split the tuple
(split/$separator/)[$n]
get the n+1th element of the tuple (remember arrays start at index zero)
(map{(split /$separator/ )[$n] }@{$rsrc})
apply this to every element of the source list.
Since we use a reference, I tell Perl this is an array ref with the syntax @{$arrayref}
[(map { (split /$separator/ )[$n] } @{$rsrc} )];
Make this brand new shiny list a reference
push @res , [(map { (split /$separator/ )[$n] } @{$rsrc} )];
so that we can add it to our result list (otherwise, we'd get a flattened list, instead of a list of lists)

As you see with the output, we got what we wanted.
I hope this could will be useful (and added to List::Utils or Perl6 after cleaning/rewriting? I didn't search a lot, but there didn't seem to be such a module on CPAN) and that my explainations will help those who, like me, are still a bit afraid of lists of lists and references...

P!