Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Invert CSV

by ig (Vicar)
on May 30, 2011 at 20:00 UTC ( #907363=note: print w/ replies, xml ) Need Help??


in reply to Invert CSV

It looks to me like you have your iterations over rows and columns ($z and $v) reversed. Maybe something like the following would work better.

use strict; use warnings; my @data; while (<DATA>) { chomp; push(@data, [ split /,/ ]); } my $cols = $#{$data[0]}; my $rows = $#data; foreach my $col (0..$cols) { foreach my $row (0..$rows) { print "$data[$row][$col],"; } print "\n"; } __DATA__ 1.1,1.2,1.3,1.4,1.5 2.1,2.2,2.3,2.4,2.5 3.1,3.2,3.3,3.4,3.5

Are you sure every row has the same number of fields?


Comment on Re: Invert CSV
Download Code
Re^2: Invert CSV
by roadtest (Sexton) on May 31, 2011 at 16:27 UTC
    Yes, you are right. I made logic mistake. I should loop column number first, then loop row number inside it. My testing data happens to be a square matrix, same columns and row numbers, which hides the logic error.

    After correcting it, all CSV files are processed correctly.

    Cheers,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907363]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-07-29 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls