Loading ...
Sorry, an error occurred while loading the content.

how to rotate csv file? (cols<<-->>rows)

Expand Messages
  • Wolcott, Kenneth A
    Hi; I have a simple csv file (ie: no embedded quotes and no embedded commas) which has just a few rows but many columns. I have a header row that starts with
    Message 1 of 2 , Jan 4, 2006
    • 0 Attachment
      Hi;

      I have a simple csv file (ie: no embedded quotes and no embedded
      commas) which has just a few rows but many columns. I have a header row
      that starts with "###". I'd like to "rotate" the csv file so that the
      columns become rows and the rows become columns. I think that the
      problem I'm having in doing this is as follows:

      1> I searched on CPAN for "rotate csv". I found several csv routines,
      none of which seem to have a rotate method, although there is a row
      parse method.
      2> I think that I will need a 2d array, which I have trouble
      conceptualizing in perl (I'll search in perldoc for data structures
      again).

      Here's some very rudimentary code:

      ############################
      use strict;
      use warnings;

      # Perl script to rotate a CSV file (rows become columns and columns
      become rows)

      my $indata = "foobar.dat";
      my $outdata = "newfoobar.dat";
      open (INDATA, "<".$indata) or die "ERROR: Unable to open " . $indata . "
      for reading...";
      open (OUTDATA, ">".$outdata) or die "ERROR: Unable to open " . $outdata
      . " for writing...";
      while (my $line = <INDATA>) {
      my @rows = split /,/, $line;
      # now I have a row, which I can print out in a column, but how to
      handle more than one row?
      for (my $i=0; $i<@rows; $i++) {
      print $row[$i]."\n";
      }
      }
      close (INDATA);
      close (OUTDATA);
      ############################

      Thanks in advance for the help,
      Ken Wolcott



      [Non-text portions of this message have been removed]
    • Damien Carbery
      ... perldoc perldsc http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perldsc.html Use an array of arrays. When you read a line (columns), push each
      Message 2 of 2 , Jan 4, 2006
      • 0 Attachment
        --- In perl-beginner@yahoogroups.com, "Wolcott, Kenneth A"
        <kwolcott@a...> wrote:
        >
        > Hi;
        >
        > I have a simple csv file (ie: no embedded quotes and no embedded
        > commas) which has just a few rows but many columns. I have a header row
        > that starts with "###". I'd like to "rotate" the csv file so that the
        > columns become rows and the rows become columns. I think that the
        > problem I'm having in doing this is as follows:
        >
        > 1> I searched on CPAN for "rotate csv". I found several csv routines,
        > none of which seem to have a rotate method, although there is a row
        > parse method.
        > 2> I think that I will need a 2d array, which I have trouble
        > conceptualizing in perl (I'll search in perldoc for data structures
        > again).
        >
        > Here's some very rudimentary code:
        >
        > ############################
        > use strict;
        > use warnings;
        >
        > # Perl script to rotate a CSV file (rows become columns and columns
        > become rows)
        >
        > my $indata = "foobar.dat";
        > my $outdata = "newfoobar.dat";
        > open (INDATA, "<".$indata) or die "ERROR: Unable to open " . $indata . "
        > for reading...";
        > open (OUTDATA, ">".$outdata) or die "ERROR: Unable to open " . $outdata
        > . " for writing...";
        > while (my $line = <INDATA>) {
        > my @rows = split /,/, $line;
        > # now I have a row, which I can print out in a column, but how to
        > handle more than one row?
        > for (my $i=0; $i<@rows; $i++) {
        > print $row[$i]."\n";
        > }
        > }
        > close (INDATA);
        > close (OUTDATA);
        > ############################
        >
        > Thanks in advance for the help,
        > Ken Wolcott

        perldoc perldsc
        http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perldsc.html
        Use an array of arrays.

        When you read a line (columns), push each element onto the different
        rows of the array of arrays.
        Probably something like this untested code:
        my @AoA; # Array of Arrays.
        while (my $line = <INDATA>) {
        my @rows = split /,/, $line;
        for (my $i=0; $i<@rows; $i++) {
        push( @{ $AoA[$i] }, $row[$i] ); # Append to a row.
        }
        }
      Your message has been successfully submitted and would be delivered to recipients shortly.