Jordan
Jordan

Reputation: 311

Array elements are not printed in Perl

I have two sets of files. One file gives a list of gene names (one gene per line). The second file has a list of gene pairs (e.g., => '1,2' and one gene pair perl line). The gene names are numerical. I want to list all possible gene combinations except the known gene pairs.

My output should be:

3,4  
4,5  
6,7  
...  
...  

But, I get something like this =>

,4  
,5  
,7  

All the first elements do not print. I'm not sure exactly what's wrong with the code. Can anyone help?

My code:

#! usr/bin/perl

use strict;
use warnings;

if (@ARGV !=2) {
   die "Usage: generate_random_pairs.pl <entrez_genes> <known_interactions>\n";
}
my ($e_file, $k_file) = @ARGV;

open (IN, $e_file) or die "Error!! Cannot open $e_file\n";
open (IN2, $k_file) or die "Error!! Cannot open $k_file\n";

my @e_file = <IN>; chomp (@e_file);
my @k_file = <IN2>; chomp (@k_file);

my (%known_interactions, %random_interactions);

foreach my $line (@k_file) {
    my @array = split (/,/, $line);
    $known_interactions{$array[0]} = $array[1];
}

for (my $i = 0; $i <= $#e_file; $i++) {
    for (my $j = $i+1 ; $j <= $#e_file; $j++) {
        if ((exists $known_interactions{$e_file[$i]}) && ($known_interactions{$e_file[$i]} == $e_file[$j])) {next;}
        if ((exists $known_interactions{$e_file[$j]}) && ($known_interactions{$e_file[$j]} == $e_file[$i])) {next;}
        print "$e_file[$i],$e_file[$j]\n";
    }
}

Upvotes: 0

Views: 397

Answers (1)

ikegami
ikegami

Reputation: 385655

Your file uses CR LF for line endings, but you're on a system that uses LF for line endings, so your program outputs

"3" <CR> "," "4" <CR> <LF>

which your terminal shows as

,4

Either fix the line endings using

dos2unix inputfile

Or change

chomp(@e_file);
chomp(@k_file);

to

s/\s+\z// for @e_file;
s/\s+\z// for @k_file;

Upvotes: 3

Related Questions