Perl:将2个数组散列与另一个数组进行比较

时间:2012-08-26 15:59:06

标签: arrays perl hash

我在Perl中编写了下面的代码,但它没有给出理想的输出。我正在处理一个数组和两个数组哈希之间的比较。

给出样本输入文件:

1) file1.txt
   A6416    A2318
   A84665   A88

2)hashone.pl

%hash1=(
A6416=>['E65559', 'C11162.1', 'c002gnj.3',],
A88=>['E77522', 'M001103', 'C1613.1', 'c001hyf.2',],
A84665=>['E138347', 'M032578', 'C7275.1', 'c009xpt.3',],
A2318=>['E128591', 'C43644.1', 'C47705.1', 'c003vnz.4',],
);

3)hashtwo.pl

%hash2=(
15580=>['C7275.1', 'E138347', 'M032578', 'c001jnm.3', 'c009xpt.2'],
3178=>['C1613.1', 'E77522','M001103', 'c001hyf.2', 'c001hyg.2'],
24406=>['C11162.1', 'E65559', 'M003010', 'c002gnj.2'],
12352=>['C43644.1', 'C47705.1', 'E128591','M001458', 'c003vnz.3'],
);

我的目标是实现所描述的任务:

从file1.txt,我必须在%hash1中找到相应的ID。例如,A6416(file1.txt)是%hash1中的键。接下来,我必须在%hash2中找到A6416 ['E65559','C11162.1','c002gnj.3',]的值。如果在%hash2中找到大多数(超过50%)的值,我将A6416替换为%hash2中的相应密钥。

Example:
A6416 A2318 
A84665 A88

Output:
24406 12352
15580 3178

请注意%hash1和%hash2的键不同(它们不重叠)。但价值是相同的(它们重叠)。

#!/usr/bin/perl -w
use strict;
use warnings;
open FH, "file1.txt" || die "Error\n";
my %hash1 = do 'hashone.pl';     
my %hash2 = do 'hashtwo.pl';  
chomp(my @array=<FH>);

foreach my $amp (@array)
{
    if ($amp =~ /(\d+)(\s?)/)
    {
        if (exists ($hash1{$1}))
        {
            for my $key (keys %hash2) 
            {
                for my $i ( 0 .. $#{ $hash2{$key} } ) 
                {
                    if ((@{$hash1{$1}}) eq ($hash2{$key}[$i]))
                    { 
                    print "$key";
                    }
                }
           }
        }
    }
}   
close FH;
1;

对此问题的任何指导都非常感谢。谢谢!

2 个答案:

答案 0 :(得分:3)

我认为你应该将%hash2反转到这个结构中:

$hash2{'C7275.1'} = $hash2{'E138347'} = $hash2{'M032578'}
                  = $hash2{'c001jnm.3'} = $hash2{'c009xpt.2'} = 15580;
$hash2{'C1613.1'} = $hash2{'E77522'} = $hash2{'M001103'}
                  = $hash2{'c001hyf.2'} = $hash2{'c001hyg.2'} = 3178;
$hash2{'C11162.1'} = $hash2{'E65559'}
                   = $hash2{'M003010'} = $hash2{'c002gnj.2'} = 24406;
$hash2{'C43644.1'} = $hash2{'C47705.1'} = $hash2{'E128591'}
                   = $hash2{'M001458'} = $hash2{'c003vnz.3'} = 3178;

这样您就可以更有效地执行这些查找,而不必遍历 <{1}}的每个元素。

答案 1 :(得分:1)

基于ruakh和zock的响应,这里是为hash2构建查找表所需的代码

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %hash2=(
15580=>['C7275.1', 'E138347', 'M032578', 'c001jnm.3', 'c009xpt.2'],
3178=>['C1613.1', 'E77522','M001103', 'c001hyf.2', 'c001hyg.2'],
24406=>['C11162.1', 'E65559', 'M003010', 'c002gnj.2'],
12352=>['C43644.1', 'C47705.1', 'E128591','M001458', 'c003vnz.3'],
);

# Build LUT for hash2
my %hash2_lut;
foreach my $key (keys %hash2)
{
    foreach my $val (@{$hash2{$key}})
    {
        $hash2_lut{$val} = $key
    }
}

print Dumper(\%hash2_lut);

请选择ruakh的帖子作为答案,只是试图为您澄清代码。使用Data::Dumper ...这是你的朋友。

这是输出:

$VAR1 = {
      'C47705.1' => '12352',
      'M032578' => '15580',
      'E138347' => '15580',
      'E77522' => '3178',
      'C7275.1' => '15580',
      'c001jnm.3' => '15580',
      'E65559' => '24406',
      'C1613.1' => '3178',
      'M001458' => '12352',
      'c002gnj.2' => '24406',
      'c009xpt.2' => '15580',
      'c001hyf.2' => '3178',
      'C43644.1' => '12352',
      'E128591' => '12352',
      'c001hyg.2' => '3178',
      'M003010' => '24406',
      'c003vnz.3' => '12352',
      'C11162.1' => '24406',
      'M001103' => '3178'
    };