Tomb Tomb - 2 years ago 92
Perl Question

select lines on tab delimited file

I'm trying to print only lines that have chrX or chrY and are positive in column 4 in a file separated by \t.


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

my code


use strict;
use warnings;

print "type in the path of the file\n";
my $file_name = <>;

open (FILE, $file_name) or die "#!";

my @line;
my @array1;

@line = split(/\t/);
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";


but I get an error

Use of uninitialized value $chr in concatenation (.) or string at line 34, <FILE> line 61287.

I've tried some modifications but it was only printing

chrX +

and not the whole line. What should I change? Thanks.

Answer Source

All of your tests should be inside the while loop, not outside. You use too much variables that seem useless. Using $_ will make your code shorter and more readable:

use strict;
use warnings;

print "Type in the path of the file:\n";
my $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 "+");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download