Perl transpose row to column

perl transpose array
awk transpose column to row
datamash transpose

I have been searching around for some ways to transpose a large csv file using Perl, but can't get it right with my loops.

There is a header row with 7 columns (I have 200+ columns actually). The first 3 columns are fixed and the subsequent columns are numbers. If the account amount is 0, skip and do not transpose.

Source Data:

Name,Age,Gender,Acct1,Acct2,Acct3,Acct4
Jack,12,M,10,20,0,999
Mary,20,F,40,50,0,111

Transposed Data:

Column_ID,Name,Age,Gender,Acct
4,Jack,12,M,10
5,Jack,12,M,20
7,Jack,12,M,999
4,Mary,20,F,40
5,Mary,20,F,50
7,Mary,20,F,111

I'm guessing this source data is in a file, rather than handily parsed into a perl assignment.

#!/usr/bin/perl

use strict;
use warnings;

print "Column_ID,Name,Age,Gender,Acct\n";
foreach my $file (@ARGV) {
    open my $FH, '<', $file
    or warn("Couldn't open $file: $!\n"), next;

    while (<$FH>) {
        chomp;
        my @cols = split /\,/;
        my @retained = @rows[0 .. 2];
        foreach my $col (3 .. $#cols) {
            print join(',', 1 + $col, @retained, $cols[$col]) . "\n"
            if $cols[$col];
        }
    }
}

Array::Transpose - Transposes a 2-Dimensional Array, The transpose function assumes all rows have the same number of columns as the DavisNetworks.com supports all Perl applications including this package. Transposing data from columns to rows I have to create a Perl script which will transpose the data output from my experiment, from columns to rows, in order for me to analyse the data. I am a complete Perl novice so any help would be greatly appreciated.


Using Perl one-liner

$ cat liquan.txt
Name,Age,Gender,Acct1,Acct2,Acct3,Acct4
Jack,12,M,10,20,0,999
Mary,20,F,40,50,0,111
$ perl -F, -lane ' BEGIN { print "Column_ID,Name,Age,Gender,Acct" } for(3..$#F) { if($F[$_]!=0 and $.>1) { print $_+1,",$F[0],$F[1],$F[2],",$F[$_] }}' liquan.txt
Column_ID,Name,Age,Gender,Acct
4,Jack,12,M,10
5,Jack,12,M,20
7,Jack,12,M,999
4,Mary,20,F,40
5,Mary,20,F,50
7,Mary,20,F,111
$

Help for a Perl newcomer! Transposing data from columns to rows , I have to create a Perl script which will transpose the data output from my experiment, from columns to rows, in order for me to analyse the data. I am a complete  Step 1: Open the data in Power Query Open the data set in Power Query that you want to transpose. The picture below shows the data that we want to transpose. We will transpose this data to rotate it from rows to columns.


Assuming that you have read the CSV into an array of arrays with one of the CSV modules (please don't parse CSV yourself), I would proceed like this:

#!/usr/bin/perl
use strict;
use warnings;

my @rows = (
    ['Jack',12,'M',10,20,0,999],
    ['Mary',20,'F',40,50,0,111],
    );

my @output;
foreach my $row (@rows) {
   foreach my $col (3..$#{$row}) {
       if ($row->[$col] != 0) {
           push(@output, [$col + 1, @{$row}[0,1,2,$col]]);
       }
   }
}

foreach my $row (@output) {
    print join(',', @{$row}), "\n";
}

Example output:

$ perl dummy.pl
4,Jack,12,M,10
5,Jack,12,M,20
7,Jack,12,M,999
4,Mary,20,F,40
5,Mary,20,F,50
7,Mary,20,F,111

Transpose a file, Dear Monks, I am currently trying to transpose a file. The input file I have is +txt'\​n"; for my $row (@rows) { for my $column (0 .. $#{$row})  Use the T attribute or the transpose () method to swap (= transpose) the rows and columns of pandas.DataFrame. Neither method changes the original object, but returns a new object with the rows and columns swapped (= transposed object). Note that depending on the data type dtype of each column, a view is created instead of a copy, and changing the value of one of the original and transposed objects will change the other.


Transpose a Matrix (Arrays or Arrays), While Perl arrays can only be one-dimensionals, each value can be a reference to another array and A matrix can be transposed. (Replace the rows by arrays.)​. The XML option to transposing rows into columns is basically an optimal version of the PIVOT in that it addresses the dynamic column limitation. The XML version of the script addresses this limitation by using a combination of XML Path, dynamic T-SQL and some built-in T-SQL functions such as STUFF and QUOTENAME .


transpose(), If you use the single argument piddle form (top row in the usage table) the output will transpose. transpose rows and columns. $y = transpose($w); pdl> $w  The array is rotated in this way: the first row of the original array becomes the first column of the new array, the second row becomes the second column, and so on. Important note! For the TRANSPOSE function to work, you must enter it as an array formula by pressing Ctrl + Shift + Enter .


A transpose tool, A simple transpose tool written in Perl. Each row of $data, contains the columns of data for that row. So $data->[0]->[2] would be “three” (first  I am newbie with Perl. I have an assignment to transpose rows into columns of a huge data. customers goods transportation ---------- ----- -------------- A, B, C, D