简体   繁体   English

读取CSV文件并保存为2 d阵列

[英]Read CSV file and save in 2 d array

I am trying to read a huge CSV file in 2 D array, there must be a better way to split the line and save it in the 2 D array in one step :s Cheers 我试图在二维数组中读取一个巨大的CSV文件,必须有一个更好的方法来分割线并一步保存在二维数组中:s干杯

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++;    
}

CSV is not trivial. CSV不是一件容易的事。 Don't parse it yourself. 不要自己解析。 Use a module like Text::CSV , which will do it correctly and fast. 使用像Text :: CSV这样的模块,它可以正确快速地完成。

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;
}

That will get all your rows nicely in @data , without worrying about parsing CSV yourself. 这将在@data很好地获取所有行,而不必担心自己解析CSV。

If you ever find yourself reaching for the C-style for loop, then there's a good chance that your program design can be improved. 如果您发现自己正在寻找C风格的循环,那么您的程序设计很有可能得到改进。

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 with a simple split (ie the records contain no embedded commas). 这假设您有一个可以使用简单split处理的CSV文件(即记录中不包含嵌入的逗号)。

Aside: You can simplify your code with: 旁白:您可以使用以下方法简化代码:

my @cols = split /,/;

Your assignment to $array[$col][$row] uses an unusual subscript order; 你对$array[$col][$row]赋值使用了一个不寻常的下标顺序; it complicates life. 它使生活变得复杂。 With your column/row assignment order in the array, I don't think there's a simpler way to do it. 根据数组中的列/行分配顺序,我认为没有更简单的方法。


Alternative: If you were to reverse the order of the subscripts in the array ( $array[$row][$col] ), you could think about using: 替代方案:如果你要颠倒数组中的下标顺序( $array[$row][$col] ),你可以考虑使用:

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";
    }
}

Sample Data 样本数据

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

Sample Output 样本输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM