Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Column Data to Row Data

by debiandude (Scribe)
on Aug 24, 2010 at 15:20 UTC ( #856960=perlquestion: print w/replies, xml ) Need Help??

debiandude has asked for the wisdom of the Perl Monks concerning the following question:

Right now I have to arrays with column data and I need to turn it into row data. I'm currently doing this to make the transformation:
my @col1 = qw /A B C/; my $otherColumns = [ [ 1, 2, 3], [4, 5, 6] ]; my @rows; foreach my $i ( 0 .. $#col1) { push @rows, [ $col[$i], map { $_->[$i] } @$otherColumns ]; }
Is there a one liner for this? For some reason this implementation feels bad. Thanks.

Replies are listed 'Best First'.
Re: Column Data to Row Data
by BioLion (Curate) on Aug 24, 2010 at 15:42 UTC

    I am sure there would be something out there on cpan which would do the table manipulation, or some very clever one liner, but I don't see any problem with your approach - it is simple, readable and in six months time you'll still know what it is doing... TIMTOWDTI, but the important thing is that it works!(disclaimer: admittedly I am assuming your way works, I haven't tested it, but it looks like it should work...).

    Maybe it could be wrapped in a subroutine, or made into an object/method, but in principle your approach is sound.

    Without trying to be your Perl-shrink, why does it feel wrong? Nothing wrong with simple.

    Just a something something...
      Haha. That's exactly what I wanted, a Perl-shrink. I honestly don't know why. I was just staring at my code and I kept asking myself if I could do this better. Not that I needed to or for performance reasons, but because it just "felt" bad. Maybe I need to up my meds :) Thanks for the reassurance. I guess I'm just officially crazy now.
Re: Column Data to Row Data
by JavaFan (Canon) on Aug 24, 2010 at 16:14 UTC
    You were almost there:
    my @rows = map {my $i = $_; [$col1[$i], map {$_->[$i]} @$otherColumns] +} 0 .. $#col1;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856960]
Approved by BioLion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-06-03 14:53 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (25 votes). Check out past polls.