【问题标题】:Array elements are not printed in PerlPerl 中不打印数组元素
【发布时间】:2012-07-31 03:09:55
【问题描述】:

我有两组文件。一个文件给出了基因名称列表(每行一个基因)。第二个文件有一个基因对列表(例如,=> '1,2' 和一个基因对 perl 行)。基因名称是数字的。我想列出除已知基因对之外的所有可能的基因组合。

我的输出应该是:

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

但是,我得到了这样的东西 =>

,4  
,5  
,7  

所有第一个元素都不打印。我不确定代码到底有什么问题。任何人都可以帮忙吗?

我的代码:

#! 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";
    }
}

【问题讨论】:

  • 你能展示一些输入文件的例子吗?每个2-3行?
  • Ikegami 解决了这个问题。但是,这里是格式。对于 entrez => 1\n 2\n 3\n 对于 known_interactions => 1,2\n 2,3\n 4,5\n
  • 应该是这样的,但它是 1\r\n2\r\n3\r\n 和 1,2\r\n2,3\r\ n4,5\r\n

标签: arrays perl printing newline


【解决方案1】:

您的文件使用 CR LF 作为行尾,但您的系统使用 LF 作为行尾,因此您的程序输出

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

您的终端显示为

,4

要么修复行尾,要么使用

dos2unix inputfile

或者改变

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-01
    • 2011-03-23
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2013-09-12
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多