Perl grep未返回预期值
我有以下代码:Perl grep未返回预期值,perl,Perl,我有以下代码: #!/usr/bin/perl # splits.pl use strict; use warnings; use diagnostics; my $pivotfile = "myPath/Internal_Splits_Pivot.txt"; open PIVOTFILE, $pivotfile or die $!; while (<PIVOTFILE>) { # loop through each line in file next if ($.
#!/usr/bin/perl
# splits.pl
use strict;
use warnings;
use diagnostics;
my $pivotfile = "myPath/Internal_Splits_Pivot.txt";
open PIVOTFILE, $pivotfile or die $!;
while (<PIVOTFILE>) { # loop through each line in file
next if ($. == 1); # skip first line (contains business segment code)
next if ($. == 2); # skip second line (contains transaction amount text)
my @fields = split('\t',$_); # split fields for line into an array
print scalar(grep $_, @fields), "\n";
}
我希望perl脚本的输出是:2 3 4
,给定每行中定义的元素数量。该文件是一个以制表符分隔的文本文件,共有8列
相反,我得到了34
,我不知道为什么
作为背景,我正在使用作为开发的基础,因为我试图计算行中的元素数,以了解是否需要跳过该行。问题应该在这一行:
my @fields = split('\t',$_); # split fields for line into an array
制表符字符不会被插值。而且您的文件似乎并不是只分开制表符,至少在这里如此。我更改了拆分正则表达式以匹配任意空格,在我的计算机上运行了代码,得到了“正确”的结果:
结果:
2
3
3
4
问题应该在这一行:
my @fields = split('\t',$_); # split fields for line into an array
制表符字符不会被插值。而且您的文件似乎并不是只分开制表符,至少在这里如此。我更改了拆分正则表达式以匹配任意空格,在我的计算机上运行了代码,得到了“正确”的结果:
结果:
2
3
3
4
我怀疑你在某些地方有与制表符混合的空间,你的GRIP测试会考虑“真”。 什么是:
use Data::Dumper;
$Data::Dumper::Useqq=1;
print Dumper [<PIVOTFILE>];
使用数据::转储程序;
$Data::Dumper::Useqq=1;
打印转储程序[];
显示?
< P>我怀疑你在某些地方有与制表符混合的空格,你的GRIP测试会考虑“真”。 什么是:use Data::Dumper;
$Data::Dumper::Useqq=1;
print Dumper [<PIVOTFILE>];
使用数据::转储程序;
$Data::Dumper::Useqq=1;
打印转储程序[];
显示?不仅有选项卡,还有空格 尝试按空间分割的方法是可行的 往下看
#!/usr/bin/perl
# splits.pl
use strict;
use warnings;
use diagnostics;
while (<DATA>) { # loop through each line in file
next if ($. == 1); # skip first line (contains business segment code)
next if ($. == 2); # skip second line (contains transaction amount text)
my @fields = split(" ",$_); # split fields by SPACE
print scalar(@fields), "\n";
}
__DATA__
4 G I M N U X
Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount
0000-13-I21 600
0001-8V-034BLA 2,172 2,172
0001-8V-191GYG 13,125 4,375
0001-9W-GH5B2A -2,967.09 2,967.09 25.00
不仅有选项卡,还有空格 尝试按空间分割的方法是可行的 往下看
#!/usr/bin/perl
# splits.pl
use strict;
use warnings;
use diagnostics;
while (<DATA>) { # loop through each line in file
next if ($. == 1); # skip first line (contains business segment code)
next if ($. == 2); # skip second line (contains transaction amount text)
my @fields = split(" ",$_); # split fields by SPACE
print scalar(@fields), "\n";
}
__DATA__
4 G I M N U X
Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount Transaction Amount
0000-13-I21 600
0001-8V-034BLA 2,172 2,172
0001-8V-191GYG 13,125 4,375
0001-9W-GH5B2A -2,967.09 2,967.09 25.00
你的密码。问题可能是输入文件包含一些“隐藏”的空白字段(例如,除制表符外的其他空白)。比如说
提供两个字段,A
和A
给出三个,AB
,A
,B
(请记住,行尾是输入的一部分!)
scalar(grep /\S/, @fields)
应该这样做。您的代码。问题可能是输入文件包含一些“隐藏”的空白字段(例如,除制表符外的其他空白)。比如说
提供两个字段,A
和A
给出三个,AB
,A
,B
(请记住,行尾是输入的一部分!)
scalar(grep /\S/, @fields)
应该这样做。作为旁注:
作为背景,我使用作为我开发的基础,因为我试图计算行中的元素数,以了解是否需要跳过该行
现在我明白了为什么要使用grep
来计算数组元素。当数组包含以下未定义的值时,这一点很重要:
my @a;
$a[1] = 42; # @a contains the list (undef, 42)
say scalar @a; # 2
或者手动删除条目时:
my @a = split /,/ => 'foo,bar'; # @a contains the list ('foo', 'bar')
delete $a[0]; # @a contains the list (undef, 'bar')
say scalar @a; # 2
但在许多情况下,尤其是当您使用数组只存储列表而不对单个数组元素进行操作时,scalar@a
可以完美地工作
my @a = (1 .. 17, 1 .. 25); # (1, 2, ..., 17, 1, 2, .., 25)
say scalar @a; # 42
了解grep
的功能非常重要!就你而言
print scalar(grep $_, @fields), "\n";
grep
返回@字段的true值列表
,然后打印您拥有的值。但有时这并不是你想要/期望的:
my @things = (17, 42, 'foo', '', 0); # even '' and 0 are things
say scalar grep $_ => @things # 3!
因为在Perl中,空字符串和数字0是假值,所以用这个习惯用法不会计算它们。因此,如果您想知道数组的长度,只需使用
say scalar @array; # number of array entries
如果要计算true值,请使用
say scalar grep $_ => @array; # number of true values
say scalar grep defined($_) => @array; # number of defined values
但是,如果要计算定义的值,请使用
say scalar grep $_ => @array; # number of true values
say scalar grep defined($_) => @array; # number of defined values
我很确定你已经从链接页面上的其他答案中知道了这一点。在散列中,情况稍微复杂一点,因为将某物设置为unde
与delete
ing它不同:
my %h = (a => 0, b => 42, c => 17, d => 666);
$h{c} = undef; # still there, but undefined
delete $h{d}; # BAM! $h{d} is gone!
当我们尝试计算值时会发生什么
say scalar grep $_ => values %h; # 1
因为42是%h
中唯一的真值
say scalar grep defined $_ => values %h; # 2
因为0已定义,尽管它为false
say scalar grep exists $h{$_} => qw(a b c d); # 3
因为可以存在未定义的值。结论:
知道你在做什么而不是复制粘贴代码片段:)作为旁注:
作为背景,我使用作为我开发的基础,因为我试图计算行中的元素数,以了解是否需要跳过该行
现在我明白了为什么要使用grep
来计算数组元素。当数组包含以下未定义的值时,这一点很重要:
my @a;
$a[1] = 42; # @a contains the list (undef, 42)
say scalar @a; # 2
或者手动删除条目时:
my @a = split /,/ => 'foo,bar'; # @a contains the list ('foo', 'bar')
delete $a[0]; # @a contains the list (undef, 'bar')
say scalar @a; # 2
但在许多情况下,尤其是当您使用数组只存储列表而不对单个数组元素进行操作时,scalar@a
可以完美地工作
my @a = (1 .. 17, 1 .. 25); # (1, 2, ..., 17, 1, 2, .., 25)
say scalar @a; # 42
了解grep
的功能非常重要!就你而言
print scalar(grep $_, @fields), "\n";
grep
返回@字段的true值列表
,然后打印您拥有的值。但有时这并不是你想要/期望的:
my @things = (17, 42, 'foo', '', 0); # even '' and 0 are things
say scalar grep $_ => @things # 3!
因为在Perl中,空字符串和数字0是假值,所以用这个习惯用法不会计算它们。因此,如果您想知道数组的长度,只需使用
say scalar @array; # number of array entries
如果要计算true值,请使用
say scalar grep $_ => @array; # number of true values
say scalar grep defined($_) => @array; # number of defined values
但是,如果要计算定义的值,请使用
say scalar grep $_ => @array; # number of true values
say scalar grep defined($_) => @array; # number of defined values
我很确定你已经从链接页面上的其他答案中知道了这一点。在散列中,情况稍微复杂一点,因为将某物设置为unde
与delete
ing它不同:
my %h = (a => 0, b => 42, c => 17, d => 666);
$h{c} = undef; # still there, but undefined
delete $h{d}; # BAM! $h{d} is gone!
当我们尝试计算值时会发生什么
say scalar grep $_ => values %h; # 1
因为42是唯一正确的<