我有一个我想要放入Perl哈希的数据列表
档案 missing_rs.txt
rs11273140
rs79236118
rs63414861
rs11414981
...
所以我构建了一个while
循环
#! usr/bin/perl
use warnings;
use strict;
my (%hash1, %hash2, %hash3); #%hash1, %hash3 are for other purposes
open (my $FH2, '<', 'missing_rs.txt') or die $!;
my $count2 = 0;
while ( my $line2 = <$FH2> ) {
chomp $line2;
my @key2 = split /\t/, $line2;
$hash2{$key2[0]} = 1;
$count2++;
}
my @missing_rs_keys = keys %hash2; # added to help troubleshoot
my @missing_rs_values = values %hash2; # added to help troubleshoot
my $item;
print "@missing_rs_keys";
my $missing_rs = scalar @missing_rs_keys; #added to help troubleshoot
print "Total missing rs (non-redundant) count is $count2\n"; # added to help troubleshoot
print "Total number of missing rs after buidling hash is $missing_rs\n"; # added to help troubleshoot
我的问题是,为什么我不能print
%hash2
个密钥,因为print "@missing_rs_keys";
的输出为空但$missing_rs = 20
。
我尝试构建一个foreach
循环来打印%hash2
中的每个密钥,循环运行良好并给了我预期的结果,但不知怎的,我不能直接print "@missing_rs_keys"
。
更新
当我运行脚本时,我得到了这个输出
[chenb07@minerva4 ~]$ perl crazy2
Total missing rs (non-redundant) count is 23
Total number of missing rs after buidling hash is 20
[chenb07@minerva4 ~]$
答案 0 :(得分:1)
您正在非Windows环境中处理Windows文本文件,因此您的chomp
正在删除每条记录上的尾随换行符,但保留回车符。这会使控制台上的每一行输出都覆盖前一行,所以看起来好像数组中没有任何内容
只需将您的chomp
替换为s/\s+\z//
,一切就会好起来