### comment on

 Need Help??
```
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!

In reply to Transposer by Pied

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-02-05 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?