Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) 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":



  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2020-06-03 07:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?



      Results (21 votes). Check out past polls.

      Notices?