Perl 在制表符分隔的文件上选择行
我试图只打印包含chrX或chrY且在文件第4列中为正数的行,\t分隔 输入 输出Perl 在制表符分隔的文件上选择行,perl,Perl,我试图只打印包含chrX或chrY且在文件第4列中为正数的行,\t分隔 输入 输出 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 (
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;
而不是整条线。我应该换什么?谢谢。您的所有测试都应该在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);
#/usr/bin/perl
严格使用;
使用警告;
打印“在文件路径中键入:\n”;
我的$filename=;
chomp($filename);
打开我的$fh,“所有测试都应该在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);
#/usr/bin/perl
严格使用;
使用警告;
打印“在文件路径中键入:\n”;
我的$filename=;
chomp($filename);
打开我的$fh,'
chrX +
#!/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);