我试图仅打印具有chrX或chrY的行,并且在由\ t分隔的文件中的第4列中为正。
输入
1373 NM_016303 chrX + 103356451 10335846
1059 NM_015666 chr20 + 62183024 62202754
116 NM_015340 chr3 + 45388582 45548836
10 NM_001206850 chrY - 14522607 14843968
输出
1373 NM_016303 chrX + 103356451 10335846
我的代码
#!/usr/bin/perl
use strict;
use warnings;
print "type in the path of the file\n";
my $file_name = <>;
chomp($file_name);
open (FILE, $file_name) or die "#!";
my @line;
my @array1;
while(<FILE>){
@line = split(/\t/);
$array1[2]=$line[2];
$array1[3]=$line[3];
}
my $positive;
my $chr;
#select only positives
if ($line[3] =~ m/\+/i ) {
$positive = $array1[3];
}
#only chrX or chrY
elsif ($line[2] =~ m/chrX/i or $line[2] =~ m/chrY/i ) {
$chr = $array1[2];
}
else {
print "no chrY or chrX\n";
}
print "$chr $positive\n";
close(FILE);
exit;
但是我收到了错误
Use of uninitialized value $chr in concatenation (.) or string at file.pl line 34, <FILE> line 61287.
我尝试了一些修改,但它只是打印
chrX +
而不是整条线。我应该改变什么?感谢。
答案 0 :(得分:0)
所有测试都应该在while循环中,而不是在外部。你使用了太多看起来毫无用处的变量。使用$_
将使您的代码更短,更易读:
#!/usr/bin/perl
use strict;
use warnings;
print "Type in the path of the file:\n";
my $filename = <>;
chomp($filename);
open my $fh, '<', $filename
or die "$!";
while(<$fh>) {
# split $_ (the current line) on whitespaces
my @fields = split;
# print $_ if the condition is true
print if ($fields[2] =~ /^chr[XY]$/ and $fields[3] eq "+");
}
close($fh);