简体   繁体   English

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

[英]Perl: Comparing 2 hash of arrays with another array

I have written the code below in Perl but it's not giving the desirable output. 我在Perl中编写了下面的代码,但它没有给出理想的输出。 I am dealing with the comparison between one array and two hash of arrays. 我正在处理一个数组和两个数组哈希之间的比较。

Given sample input files: 给定样本输入文件:

1) file1.txt
   A6416    A2318
   A84665   A88

2) hashone.pl 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 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'],
);

My aim is to achieve the task described: 我的目标是实现所描述的任务:

From file1.txt, I have to locate the corresponding ID in %hash1. 从file1.txt,我必须在%hash1中找到相应的ID。 For instance,A6416 (file1.txt) is the key in %hash1. 例如,A6416(file1.txt)是%hash1中的键。 Next, I have to find the values of A6416 ['E65559', 'C11162.1', 'c002gnj.3',] in %hash2. 接下来,我必须在%hash2中找到A6416 ['E65559','C11162.1','c002gnj.3',]的值。 If majority (more than 50%) of the values are found in %hash2, I replace A6416 with corresponding key from %hash2. 如果在%hash2中找到大多数(超过50%)的值,我将A6416替换为%hash2中的相应密钥。

Example:
A6416 A2318 
A84665 A88

Output:
24406 12352
15580 3178

Please note that the keys for %hash1 and %hash2 are different (they don't overlap). 请注意,%hash1和%hash2的键不同(它们不重叠)。 But the values are the same (they overlap). 但价值是相同的(它们重叠)。

#!/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;

Any guidance on this problem is highly appreciated. 任何关于这个问题的指导都非常感谢。 Thank you! 谢谢!

I think you should invert %hash2 into this structure: 我认为你应该将%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;

So that you can perform these look-ups much more effectively, rather than having to iterate over every element of every element of %hash2 . 这样您就可以更有效地执行这些查找,而不必遍历%hash2每个元素每个元素。

Building on the responses from ruakh and zock here is the code you need to build the look-up table for hash2 基于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);

Please select ruakh's post as the answer, just trying to clarify the code for you. 请选择ruakh的帖子作为答案,只是试图为您澄清代码。 Use Data::Dumper ...it is your friend. 使用Data::Dumper ......这是你的朋友。

Here is the output: 这是输出:

$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'
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM