http://www.perlmonks.org?node_id=474585


in reply to Transposer

You said your motivation was a need to
take a list of tuples and give back the list of all the nth elements of each tuple
but the code you posted doesn't do exactly that. (It transposes a matrix, as already discussed.) So here's code that does exactly that.
# first arg: n # second arg: array(ref) of arrays. # return: list of nth elements. sub nth_elem { map $_->[$_[0]], @{$_[1]} }
Trivial, as you can see.

Replies are listed 'Best First'.
Re^2: Transposer
by Pied (Monk) on Jul 14, 2005 at 06:29 UTC
    Well, it's not a matrix which is input, but it is a matrix that is output. input is a list of strings, with each string defining a tuple, whereas output is a LoL. Anyway, thanks for the code.
      Yeah... but as already discussed, the part that converts those strings into actual arrays should probably be separated from the other part. But hey - if they must be integral, then here:
      # first arg: n # second arg: array(ref) of strings which are comma-separated lists. # return: list of nth elements. sub nth_elemS { map { (split ',')[$_[0]] } @{$_[1]} }