你死心塌地使用bind_columns?我想我看到你想要做什么,这是理论上非常有创意,但如果你想要的是一种通过在标题名称来引用列,怎么是这样的:
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new ({ binary => 1 });
my (%header);
open my $io, "<", '/var/tmp/foo.csv' or die $!;
while (my $row = $csv->getline ($io)) {
next unless $. > 7;
my @fields = @$row;
unless (%header) {
$header{$fields[$_]} = $_ for 0..$#fields;
next;
}
my $ip_addr = $fields[$header{'IP'}];
print "$. => $ip_addr\n";
}
close $io;
样品输入:
Test Data,,,
Trash,,,
Test Data,,,
Trash,,,
Beans,Joe,10.224.38.189,XYZ
Beans,Joe,10.224.38.190,XYZ
Beans,Joe,10.224.38.191,XYZ
Last Name,First Name,IP,Computer
Beans,Joe,10.224.38.192,XYZ
Beans,Joe,10.224.38.193,XYZ
Beans,Joe,10.224.38.194,XYZ
Beans,Joe,10.224.38.195,XYZ
Beans,Joe,10.224.38.196,XYZ
Beans,Joe,10.224.38.197,XYZ
输出:
9 => 10.224.38.192
10 => 10.224.38.193
11 => 10.224.38.194
12 => 10.224.38.195
13 => 10.224.38.196
14 => 10.224.38.197
你能给从CSV几行文件的例子吗? – AKHolland
前几行是垃圾数据。我不需要第8行,它包含头(IP,DNS,MAC等)。然后在它下面有每行主机的信息。 – eagles02
阅读标题行之前,有一些内容符合'readline $ io for 1..7'的含义。 –