perl null 或空检测
我有一个包含匹配子字符串结果的哈希。如果字符串之间没有匹配,我想打印一条消息。我尝试了以下方法,但没有成功。
foreach (keys %d) {
if ($_ eq "") {
print "no matches"; # and i've tried (if defined $_
} else {
print "$_\n";
}
}
%d 是这样填充的(它包含匹配的子字符串):
foreach (my $i=0;$i<length($seq1)-$k;$i+=1) {
my $common=substr($seq1,$i,$k);
if ($seq2=~/$common/) {
$d{$common}++;
}
}
I have a hash containing results of matching substrings. I want to print a message if there is no matching between the string. I've tried the following and it didn't work.
foreach (keys %d) {
if ($_ eq "") {
print "no matches"; # and i've tried (if defined $_
} else {
print "$_\n";
}
}
the % d is filled this way (it contains matched substrings) :
foreach (my $i=0;$i<length($seq1)-$k;$i+=1) {
my $common=substr($seq1,$i,$k);
if ($seq2=~/$common/) {
$d{$common}++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我终于明白了你想要实现的目标。您认为检查
%d
中的键是否等于空字符串,则循环中没有匹配项。这是错误的。如果没有匹配项,则没有键,循环将永远不会执行。不幸的是,您无法通过这种方式检查
%d
是否不包含任何值。你需要类似的东西:I think I finally see what you are trying to accomplish. You think that checking if the keys in
%d
equal the empty string, then there were no matches in your loop. This is false. If there are no matches, then there are no keys, and the loop will never execute.Unfortunately, you cannot check if
%d
contains no values that way. You need something like:您对所有现有键进行迭代并检查它们是否是空字符串,我想这不是您想要的。
尝试一下
,或者如果它设置为“”,那么
为了更有帮助,人们必须知道你的散列是如何填充的。
您还需要初始化不匹配的值。在您的代码中,您可以添加
然后检查
You have an iteration over all existing keys and check if the are an empty string, that's I guess not what you want.
Try
or if it is set to "" then
To be more helpfull, one would have to know how your hash is filled.
You need to initialize also the non matching values. In your code you can add an
and then check