在Perl中打印数组时出现问题

时间:2014-09-04 08:34:53

标签: arrays perl hash printing

我有以下perl代码:

use strict;
use warnings;

my %hash;

open FILE, $ARGV[0];
while (my $line = <FILE>) {
    if ($line =~ /gene_type "protein_coding";/) {
        $line =~ /gene_id "([A-Za-z0-9.]*)"/;
        my $genename = $1;
        my @chomp = split(/\t/, $line);
        my @coordinates = ($chomp[3], $chomp[4]);
        if (!defined $hash{$genename}) {
            push @{$hash{$genename}}, [@coordinates];
            next;
        }
        for my $coord (@{$hash{$genename}}) {
            print $coord."\n";
        }
    }
}

此代码创建包含数组的哈希。但是,我无法打印阵列。它给出了以下错误:

Use of uninitialized value $coord[0] in concatenation (.) or string at untitled.pl line 16, <FILE> line 17813.
Use of uninitialized value $coord[0] in concatenation (.) or string at untitled.pl line 16, <FILE> line 17814.
Use of uninitialized value $coord[0] in concatenation (.) or string at untitled.pl line 16, <FILE> line 17815.
Use of uninitialized value $coord[0] in concatenation (.) or string at untitled.pl line 16, <FILE> line 17816.
Use of uninitialized value $coord[0] in concatenation (.) or string at untitled.pl line 16, <FILE> line 17817.

只需打印$coord,不使用[0]即可获得以下内容:

ARRAY(0xac5b18)
ARRAY(0xac5b18)
ARRAY(0xac5b18)
ARRAY(0xac5b18)

我的输入文件是:

chr1    HAVANA  exon    972861  973010  .   +   .   gene_id "ENSG00000187583.7"; transcript_id "ENST00000379407.4"; gene_type "protein_coding"; gene_status "KNOWN"; gene_name "PLEKHN1"; transcript_type "protein_coding"; transcript_status "KNOWN"; transcript_name "PLEKHN1-004"; exon_number 11; exon_id "ENSE00001386720.1"; level 2; protein_id "ENSP00000368717.2"; tag "basic"; tag "appris_candidate"; tag "CCDS"; ccdsid "CCDS53256.1"; havana_gene "OTTHUMG00000040756.4"; havana_transcript "OTTHUMT00000473255.1";
chr1    HAVANA  CDS 972861  973010  .   +   0   gene_id "ENSG00000187583.7"; transcript_id "ENST00000379407.4"; gene_type "protein_coding"; gene_status "KNOWN"; gene_name "PLEKHN1"; transcript_type "protein_coding"; transcript_status "KNOWN"; transcript_name "PLEKHN1-004"; exon_number 11; exon_id "ENSE00001386720.1"; level 2; protein_id "ENSP00000368717.2"; tag "basic"; tag "appris_candidate"; tag "CCDS"; ccdsid "CCDS53256.1"; havana_gene "OTTHUMG00000040756.4"; havana_transcript "OTTHUMT00000473255.1";
chr1    HAVANA  exon    973500  973640  .   +   .   gene_id "ENSG00000187583.7"; transcript_id "ENST00000379407.4"; gene_type "protein_coding"; gene_status "KNOWN"; gene_name "PLEKHN1"; transcript_type "protein_coding"; transcript_status "KNOWN"; transcript_name "PLEKHN1-004"; exon_number 12; exon_id "ENSE00001371278.1"; level 2; protein_id "ENSP00000368717.2"; tag "basic"; tag "appris_candidate"; tag "CCDS"; ccdsid "CCDS53256.1"; havana_gene "OTTHUMG00000040756.4"; havana_transcript "OTTHUMT00000473255.1";
chr1    HAVANA  CDS 973500  973640  .   +   0   gene_id "ENSG00000187583.7"; transcript_id "ENST00000379407.4"; gene_type "protein_coding"; gene_status "KNOWN"; gene_name "PLEKHN1"; transcript_type "protein_coding"; transcript_status "KNOWN"; transcript_name "PLEKHN1-004"; exon_number 12; exon_id "ENSE00001371278.1"; level 2; protein_id "ENSP00000368717.2"; tag "basic"; tag "appris_candidate"; tag "CCDS"; ccdsid "CCDS53256.1"; havana_gene "OTTHUMG00000040756.4"; havana_transcript "OTTHUMT00000473255.1";

为什么?

2 个答案:

答案 0 :(得分:4)

$coord是对数组的引用。使用arrow operator取消引用:

print $coord->[0], "\n";

perlreftut中的更多信息。

答案 1 :(得分:3)

$coord是一个数组引用。取消引用它以获得实际数组:

print "@$coord\n";

此外,在存储数组时,您将把词法数组@coordinates复制到匿名数组。这不是必需的,您可以直接存储对数组的引用,因为在循环的每次迭代中都会创建一个新的引用:

push @{ $hash{$genename} }, \@coordinates;