Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的perl脚本有什么问题?尝试使用哈希进行计数_Perl - Fatal编程技术网

我的perl脚本有什么问题?尝试使用哈希进行计数

我的perl脚本有什么问题?尝试使用哈希进行计数,perl,Perl,所以我有一个有两列的文件。我想在文件的第2列中找到所有唯一的模式,模式出现的次数以及它们在文件的第1列中对应的伙伴 下面是我的文件示例,第1列和第2列由一个选项卡分隔: OG5_126538 01111111111110 OG5_126539 01110111110100 OG5_126552 10000000000000 OG5_126558 11111111111111 OG5_126561 11111010000111 OG5_126566 01111011

所以我有一个有两列的文件。我想在文件的第2列中找到所有唯一的模式,模式出现的次数以及它们在文件的第1列中对应的伙伴

下面是我的文件示例,第1列和第2列由一个选项卡分隔:

OG5_126538  01111111111110  
OG5_126539  01110111110100  
OG5_126552  10000000000000  
OG5_126558  11111111111111  
OG5_126561  11111010000111  
OG5_126566  01111011101001  
OG5_126569  11111111111110  
OG5_126570  11111111111110  
OG5_126572  11111111111110
模式“11111111110”在第2列中出现3次,其在第1列中的关联伙伴为“OG5_126572、OG5_126570、OG5_126569”。我想要第2列中所有独特模式的信息

我编写了一个perl程序,粘贴在下面。但我总是出错。我是编程新手。我的程序有什么问题?提前谢谢你的帮助。 Perl程序:

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

if ( @ARGV < 1 ) {
    print "usage: matrix.pl filename\n";
    die;
}

my $my_file = shift;

my (%matrix_pattern);

open( SOURCE, $my_file );

while (<SOURCE>) {
    chomp;
    my ( $group, $pattern ) = split( "\t", $_ );
    $matrix_pattern{$group} = $pattern;
    $matrix_pattern{$pattern}++;
}

my @unique = values(%matrix_pattern);
my @sorted_unique = sort @unique;
foreach my $unique (@sorted_unique) {
    my $test = $matrix_pattern{$unique};
    print "$unique $test\n";
}

close SOURCE;
01110111110100 1  
01111011101001 1  
01111111111110 1  
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
10000000000000 1  
11111010000111 1  
11111111111110 3  
11111111111110 3  
11111111111110 3  
11111111111111 1  
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
3   
#/usr/local/bin/perl
严格使用;
使用警告;
如果(@ARGV<1){
打印“用法:matrix.pl文件名\n”;
死亡
}
my$my_file=shift;
我的(%matrix_模式);
开放(源代码,$my_文件);
而(){
咀嚼;
我的($group,$pattern)=拆分(“\t”,$”;
$matrix_pattern{$group}=$pattern;
$matrix_pattern{$pattern}++;
}
我的@unique=值(%matrix\u pattern);
我的@sorted_unique=排序@unique;
foreach my$unique(@sorted_unique){
my$test=$matrix_pattern{$unique};
打印“$unique$test\n”;
}
近源;
以下是程序的输出:

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

if ( @ARGV < 1 ) {
    print "usage: matrix.pl filename\n";
    die;
}

my $my_file = shift;

my (%matrix_pattern);

open( SOURCE, $my_file );

while (<SOURCE>) {
    chomp;
    my ( $group, $pattern ) = split( "\t", $_ );
    $matrix_pattern{$group} = $pattern;
    $matrix_pattern{$pattern}++;
}

my @unique = values(%matrix_pattern);
my @sorted_unique = sort @unique;
foreach my $unique (@sorted_unique) {
    my $test = $matrix_pattern{$unique};
    print "$unique $test\n";
}

close SOURCE;
01110111110100 1  
01111011101001 1  
01111111111110 1  
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
1   
10000000000000 1  
11111010000111 1  
11111111111110 3  
11111111111110 3  
11111111111110 3  
11111111111111 1  
Use of uninitialized value $test in concatenation (.) or string at matrix_sample.pl line 27, <SOURCE> line 9.  
3   
011011110100 1
01111011101001 1  
01111111111110 1  
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
1.
10000000000000 1  
11111010000111 1  
11111111111110 3  
11111111111110 3  
11111111111110 3  
11111111111111 1  
在矩阵_sample.pl第27行第9行的串联(.)或字符串中使用未初始化值$test。
3.

您试图使用散列的
值作为键。这是您警告的来源

解决您的目标的一个更简单的解决方案是使用数组散列:

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

my $fh = \*DATA;

my %matrix;

while (<$fh>) {
    chomp;
    my ($group, $pattern) = split ' ';
    push @{$matrix{$pattern}}, $group;
}

for my $pattern (sort keys %matrix) {
    print $pattern . ' for ' . @{$matrix{$pattern}} . " times.  Values are @{$matrix{$pattern}}\n";
}

__DATA__
OG5_126538 01111111111110
OG5_126539 01110111110100
OG5_126552 10000000000000
OG5_126558 11111111111111
OG5_126561 11111010000111
OG5_126566 01111011101001
OG5_126569 11111111111110
OG5_126570 11111111111110
OG5_126572 11111111111110

非常感谢你,米勒。所以这一行:“print$pattern.”for“@{$matrix{$pattern}.”次。值为@{$matrix{$pattern}}\n“;”@{$matrix{$pattern}。计数和@{$matrix{$pattern}}是否告诉我正在计数什么?如果在上下文中使用数组,它将返回元素数。这就是串联运算符
的作用。但是,如果插值数组,它将使用默认为空格的值连接元素。