Syntactic Confectionery Delight | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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
|
|