Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Question: Question: Why the Perl script flash error and print weird lines once change the operating system?

Neelam Jha
3935 days ago

Question: Why the Perl script flash error and print weird lines once change the operating system?

Hi Bioinformatician, I am working on a small script which read a file and extract the gene coordinates. It works fine when I run on linux OS but in Window OS it print some weird sign, and also does not print the expected result. Following are the code for your reference.

#!/usr/bin/perl
use strict;
use warnings;

    my $fileIn = $ARGV[0];
    my $OutFile = $ARGV[1];
    open my $fh, '<', $fileIn or die "could not open $fileIn for read\n";
    open OUTFILE, ">" , $OutFile or die "$0: open $OutFile: $!";
    while (<$fh>) {
    chomp $_;
    my $line=trim($_);
    $line =~ s/(\.\.|\s+)/\t/g;
    $line =~ s/\(|\)/\t/g;
    my $number=$.;
    my @tmp=split /\t/, $line;
    if($tmp[2] ne 'complement') {
        my @newtmp = split(/gene/, $line);
        my $newline=$newtmp[1];
        my @tmp2=split /\t/, $newline;
        my $head="gene";
        print OUTFILE "$number\t$head\t\t$tmp2[1]\t$tmp2[2]\n";
        }
    else {
        print OUTFILE "$number\t$tmp[1]\t$tmp[2]\t$tmp[3]\t$tmp[4]\n";
        }
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($) {
    my $string = shift;
    $string =~ s/^[\t\s]+//;
    $string =~ s/[\t\s]+$//;
    return $string;
}

Answers
2

I am agree with Jit, newline is the common problem in this kind of cases. I have modified your trim subroutine to remove remove odd or bad newline values from your INFILE.

# Perl trim function to remove whitespace from the start and end of the string

sub trim($){   
my $string = shift;    
$string =~ s/^[\t\s]+//;    
$string =~ s/[\t\s]+$//;    
$string =~ s/[\r\n]+$//; ## remove odd or bad newline values...    
return $string;
}

 

Hi Jitendra Narayan. Your trim subroutine works perfect. Thanks a lot.

Neelam Jha 3921 days ago

0

It looks to me that you have some newline problem. You should must check the file for it. Mostly newlines aren't a universally accepted standard; they are different depending platform (OS) : For DOS / Windows it is \r\n whereas in Unix the newline is recognized by \n.

So trim your lines for \r ;