Reading CSV file and saving to 2 d array

I am trying to read a huge CSV file in a 2 D array, there should be a better way to split the string and save it in a 2 D array in one step: s Greetings

my $j = 0; while (<IN>) { chomp ; my @cols=(); @cols = split(/,/); shift(@cols) ; #to remove the first number which is a line header for(my $i=0; $i<11; $i++) { $array[$i][$j] = $cols[$i]; } $j++; } 
+4
source share
3 answers

CSV is not trivial. Do not disassemble it by yourself. Use a module, for example Text :: CSV , that will do this correctly and quickly.

 use strict; use warnings; use Text::CSV; my @data; # 2D array for CSV data my $file = 'something.csv'; my $csv = Text::CSV->new; open my $fh, '<', $file or die "Could not open $file: $!"; while( my $row = $csv->getline( $fh ) ) { shift @$row; # throw away first value push @data, $row; } 

This will get all your lines in @data without worrying about parsing the CSV yourself.

+12
source

If you ever get into the cycle of cycle C, then there is a good chance that your program design can be improved.

 while (<IN>) { chomp; my @cols = split(/,/); shift(@cols); #to remove the first number which is a line header push @array, \@cols; } 

This assumes that you have a CSV file that can be processed using a simple split (i.e. records do not contain embedded commas).

+3
source

Additionally: you can simplify the code:

 my @cols = split /,/; 

Your assignment $array[$col][$row] uses an unusual sequence number; it makes life harder. With the order of the column / row assignment in the array, I don't think there is an easier way to do this.


Alternative: If you reordered the indexes in the array ( $array[$row][$col] ), you might consider using:

 use strict; use warnings; my @array; for (my $j = 0; <>; $j++) # For testing I used <> instead of <IN> { chomp; $array[$j] = [ split /,/ ]; shift @{$array[$j]}; # Remove the line label } for (my $i = 0; $i < scalar(@array); $i++) { for (my $j = 0; $j < scalar(@{$array[$i]}); $j++) { print "array[$i,$j] = $array[$i][$j]\n"; } } 

Data examples

 label1,1,2,3 label2,3,2,1 label3,2,3,1 

Output example

 array[0,0] = 1 array[0,1] = 2 array[0,2] = 3 array[1,0] = 3 array[1,1] = 2 array[1,2] = 1 array[2,0] = 2 array[2,1] = 3 array[2,2] = 1 
+2
source

All Articles