我有两个制表符分隔的文件:一个是带有数千个条目的引用,另一个是用于搜索引用的数百万条条件的列表。
我使用以下代码对参考文件进行哈希处理
use strict;
use warnings;
#use Data::Dumper;
#use Timer::Runtime;
use feature qw( say );
my $in_qfn = $ARGV[0];
my $out_qfn = $ARGV[1];
my $transcripts_qfn = "file";
my %transcripts;
{
open(my $transcripts_fh, "<", $transcripts_qfn)
or die("Can't open \"$transcripts_qfn\": $!\n");
while ( <$transcripts_fh> ) {
chomp;
my @refs = split(/\t/, $_);
my ($ref_chr, $ref_strand) = @refs[0, 6];
my $values = {
start => $refs[3],
end => $refs[4],
info => $refs[8]
};
#print Data::Dumper->Dump([$values]), $/; #confirm structure is fine
push @{ $transcripts{$ref_chr}{$ref_strand} }, $values;
}
}
然后我打开另一个输入文件,定义元素,并解析哈希以找到匹配条件
while ( <$in_fh> ) {
chomp;
my ($x, $strand, $chr, $y, $z) = split(/\t/, $_);
#match the reference hash for things equal to $chr and $strand
my $transcripts_array = $transcripts{$chr}{$strand};
for my $transcript ( @$transcripts_array ) {
my $start = $transcript->{start};
my $end = $transcript->{end};
my $info = $transcript->{info};
#print $info and other criteria from if statements to outfile, this code works
}
}
这有效,但我想知道我是否可以在散列中找到与$chr
匹配的元素,而不是$strand
(其二进制值为任一符号)。
我在以前的while
之后将以下内容放入相同的for
块中,但它似乎不起作用
my $transcripts_opposite_strand = $transcripts{$chr}{!$strand};
for my $transcript (@$transcripts_opposite_strand) {
my $start = $transcript->{start};
my $end = $transcript->{end};
my $info = $transcript->{info};
#print $info and other criteria from if statements
}
我为代码片段道歉;我试图保留相关信息。由于文件的大小,我不能通过逐行逐行强制它。
否定运算符!
对其参数强制执行布尔上下文。 "+"
和"-"
在布尔上下文中都是正确的,所以! $strand
总是假的,即字符串上下文中的""
。
在散列中存储布尔值
$strand = $strand eq '+';
或者不要使用布尔否定:
my $transcripts_opposite_strand = $transripts{$chr}{ $strand eq '+' ? '-' : '+' };
三元运算符可以用更短但不太可读的替代方案替换,例如,
qw( + - )[ $strand eq '+' ]
因为在数字上下文中,true被解释为1,false被解释为0。