在Perl中,如何编写和读取由密钥和数组组成的CSV文件?

时间:2022-02-08 05:43:53

I have a problem to write and read properly a CSV file composed of name(key) and array of values:

我有一个问题是要正确地编写和读取由名称(键)和值数组组成的CSV文件:

testarray.csv

foo1 ,0,0,0,0,1
foo2 ,1,0,0,0,1
foo3 ,3,4,5,6,7
.
.
.

I need to represent that file as follows:

我需要表示该文件如下:

foo# will be the key and the five following numbers will be its array.

foo#将是关键,后面的五个数字将是它的数组。

What is the simple way to conduct that and to recall it for a use (not with Dumper)? How can I use a varible from an array of a specific key?

进行该操作并将其召回以供使用(不是使用Dumper)的简单方法是什么?如何使用特定键的数组中的变量?

E.g.,

print $hsh{'foo1'}[4];

2 个解决方案

#1


Normally, I would recommend Text::xSV and/or Text::CSV but for such simple data, a straightforward join should work:

通常,我建议使用Text :: xSV和/或Text :: CSV,但对于这样的简单数据,直接连接应该有效:

#!/usr/bin/perl

use strict;
use warnings;

my %hash = (
    foo1 => [ 0, 0, 0, 0, 1 ],
    foo2 => [ 1, 0, 0, 0, 1 ],
    foo3 => [ 3, 4, 5, 6, 7 ],
);

for my $key ( sort keys %hash ) {
    print join( q{,}, $key, @{ $hash{$key} } ), "\n";
}

__END__

Output:

C:\Temp> ttt
foo1,0,0,0,0,1
foo2,1,0,0,0,1
foo3,3,4,5,6,7

Reading it in:

阅读:

#!/usr/bin/perl

use strict;
use warnings;

my %hash;

while ( <DATA> ) {
    chomp;
    last unless /\S/;
    my ($key, @data) = split /,/;
    $hash{$key} = \@data;
}

print $hash{foo2}->[4], "\n";

__DATA__
foo1,0,0,0,0,1
foo2,1,0,0,0,1
foo3,3,4,5,6,7

Output:

C:\Temp> ttt
1

#2


Sinan Unur's solution is good and correct, but I think that the OP's problem is an XY Problem. He is specifically asking about reading and storing data from a CSV file. Since that seems to be the real goal, then the best solution is to make use of reuse and get Text::CSV installed in your Perl. It does the heavy lifting of dealing with CSV files, gives you ways to reference all the data, and provides a nice API while doing so.

Sinan Unur的解决方案是好的和正确的,但我认为OP的问题是XY问题。他特别询问从CSV文件中读取和存储数据。由于这似乎是真正的目标,因此最好的解决方案是利用重用并在Perl中安装Text :: CSV。它完成了处理CSV文件的繁重工作,为您提供了引用所有数据的方法,并提供了一个很好的API。

If that doesn't float your boat, you can try DBD::CSV which, in conjunction with DBI will give you the ability to use SQL to query/insert/update the CSV. If you're used to SQL, that is a nifty way to proceed as well.

如果这不会漂浮您的船,您可以尝试DBD :: CSV,它与DBI一起使您能够使用SQL查询/插入/更新CSV。如果你已经习惯了SQL,那么这也是一种很好的方式。

Edit:

Based on simple nature of data, if you really want to roll your own, his solution is a good one, already gave it +1.

基于数据的简单性,如果你真的想要自己推出,他的解决方案是一个很好的解决方案,已经给它+1了。

#1


Normally, I would recommend Text::xSV and/or Text::CSV but for such simple data, a straightforward join should work:

通常,我建议使用Text :: xSV和/或Text :: CSV,但对于这样的简单数据,直接连接应该有效:

#!/usr/bin/perl

use strict;
use warnings;

my %hash = (
    foo1 => [ 0, 0, 0, 0, 1 ],
    foo2 => [ 1, 0, 0, 0, 1 ],
    foo3 => [ 3, 4, 5, 6, 7 ],
);

for my $key ( sort keys %hash ) {
    print join( q{,}, $key, @{ $hash{$key} } ), "\n";
}

__END__

Output:

C:\Temp> ttt
foo1,0,0,0,0,1
foo2,1,0,0,0,1
foo3,3,4,5,6,7

Reading it in:

阅读:

#!/usr/bin/perl

use strict;
use warnings;

my %hash;

while ( <DATA> ) {
    chomp;
    last unless /\S/;
    my ($key, @data) = split /,/;
    $hash{$key} = \@data;
}

print $hash{foo2}->[4], "\n";

__DATA__
foo1,0,0,0,0,1
foo2,1,0,0,0,1
foo3,3,4,5,6,7

Output:

C:\Temp> ttt
1

#2


Sinan Unur's solution is good and correct, but I think that the OP's problem is an XY Problem. He is specifically asking about reading and storing data from a CSV file. Since that seems to be the real goal, then the best solution is to make use of reuse and get Text::CSV installed in your Perl. It does the heavy lifting of dealing with CSV files, gives you ways to reference all the data, and provides a nice API while doing so.

Sinan Unur的解决方案是好的和正确的,但我认为OP的问题是XY问题。他特别询问从CSV文件中读取和存储数据。由于这似乎是真正的目标,因此最好的解决方案是利用重用并在Perl中安装Text :: CSV。它完成了处理CSV文件的繁重工作,为您提供了引用所有数据的方法,并提供了一个很好的API。

If that doesn't float your boat, you can try DBD::CSV which, in conjunction with DBI will give you the ability to use SQL to query/insert/update the CSV. If you're used to SQL, that is a nifty way to proceed as well.

如果这不会漂浮您的船,您可以尝试DBD :: CSV,它与DBI一起使您能够使用SQL查询/插入/更新CSV。如果你已经习惯了SQL,那么这也是一种很好的方式。

Edit:

Based on simple nature of data, if you really want to roll your own, his solution is a good one, already gave it +1.

基于数据的简单性,如果你真的想要自己推出,他的解决方案是一个很好的解决方案,已经给它+1了。