如何获取perl中文件中存在的最大列数

时间:2015-02-11 14:19:21

标签: perl file

我有一个test.csv文件,其中包含类似这样的数据。

"a","usa","24-Nov-2011","100.98","Extra1","Extra2"
"B","zim","23-Nov-2011","123","Extra22"
"C","can","23-Nov-2011","123"

我想获取此文件中的最大列数(在本例中为i,e 6),然后将其存储在变量中。

喜欢

 Variable=6

您能否就如何继续提供一些建议。

2 个答案:

答案 0 :(得分:5)

尝试使用Text::CSV

读取每一行,解析该模块,并将字段数与变量进行比较。

#!/bin/env perl
use strict;
use warnings;

use Text::CSV;

my $csv = Text::CSV->new;
my $max = 0;

open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
    my $count = scalar @$rows;
    $max = $count > $max ? $count : $max;
}

答案 1 :(得分:1)

人们在CSV文件而非split上使用Text::CSV的原因之一是Text::CSV不是标准的Perl模块,因此它可能无法使用。

然后使用Text::ParseWords。这是一个标准模块,应该随时可用:

#! /usr/bin/env perl
#

use strict;
use warnings;
use feature qw(say);

use Text::ParseWords qw(quotewords);

my $keep = 0;
for my $line ( <DATA> ) {
    chomp $line;
    my @columns = quotewords ("\s*,\s*", $keep, $line );
    say "<" . join( ">  <", @columns ) . ">";
}
__DATA__
"a","usa","24-Nov-2011","100.98","Extra1","Extra2"
"B","zim","23-Nov-2011","123","Extra22"
"C","can","23-Nov-2011","123"
"D","can, can, can","23-Nov-2011","123"

这会产生:

<a>  <usa>  <24-Nov-2011>  <100.98>  <Extra1>  <Extra2>
<B>  <zim>  <23-Nov-2011>  <123>  <Extra22>
<C>  <can>  <23-Nov-2011>  <123>
<D>  <can, can, can>  <23-Nov-2011>  <123>

请注意,引号内的逗号并没有抛弃解析。现在,使用split没有更多的借口了。