繁体   English   中英

Perl比较两个哈希

[英]Perl comparing two hashes

我是Perl的初学者,正在尝试编写脚本以比较两个哈希值,并在第一个哈希中打印第二个哈希中找不到的值。 尽管我知道脚本应该非常简单,但是我不确定为什么我的脚本无法正常工作。 任何帮助将非常感激。

到目前为止,我的脚本是:

#!/usr/bin/perl
use strict;
use warnings;
use vars qw($a $b $c $d $hash1 %hash1 $info1 $hash2 %hash2);

open (FILE1, "<file1.txt") || die "$!\n Couldn't open file1.txt\n";

while (<FILE1>){
    chomp (my $line=$_);
    my ($a, $b, $c, $d) = split (/\t/, $line); 
    if ($a){
         $hash1 -> {$a} -> {info1} = "$b\t$c\t$d"; 
       } 
        $info1=$hash1->{$a}->{info1};
 }



open (FILE2, "<file2.txt") || die "$!\n Couldnt open file2.txt \n";
open (Output, ">Output.txt")||die "Can't Open Output file";



while (<FILE2>) {
   chomp (my $line=$_);

   my ($a, $b, $c, $d) = split (/\t/, $line); 
   if ($a){
   $hash2 -> {$a} -> {info2} = "$b\t$c\t$d"; 
   } 


foreach (my $hash1->{$a}) {
    if (!exists $hash2{$a}) {
               print Output "$a\t$info1\n";
                      }
       }
 }

 close FILE1;
 close FILE2;
 close Output;

 print "Done!\n";

您可以从%h1获取所有键,并从%h2删除所有键,仅留下不在%h2键,

my %h1 = qw(k1 v1 k2 v2 k3 v3 k4 v4);
my %h2 = qw(k1 v1 k2 v2 k3 v3);

my %not_found_in_h2 = %h1;
delete @not_found_in_h2{keys %h2};

print "$_\n" for values %not_found_in_h2;

输出

v4

我发现您的程序中似乎有多个错误。 我已经重新格式化了您的代码,并在下面的错误的确切位置留下了注释:

#!/usr/bin/env perl

use strict;
use warnings;

use autodie;

# use 3-arg open
open my $file1, '<', 'file1.txt'; # no need to check errors with autodie

# declare your lexical variables in the proper scope    
my $hash1 = {};
while (my $line = <$file1>) {
    chomp $line;
    my ($a, $b, $c, $d) = split /\t/, $line;
    if ($a) {
         $hash1->{$a}{info1} = "$b\t$c\t$d";
    }
    # It's unclear why you are assigning to this variable?
    my $info1 = $hash1->{$a}{info1};
}

open my $file2, '<', 'file2.txt';
open my $output, '>', 'Output.txt';

# same thing here: declare your lexical variables
my $hash2 = {};
while (my $line = <$file2>) {
    chomp $line;

    my ($a, $b, $c, $d) = split (/\t/, $line);
    if ($a) {
        $hash2->{$a}{info2} = "$b\t$c\t$d";
    }

    # BUG: You can't iterate over a hash directly, but you
    # can iterate over the keys of a hash.
    foreach my $key (keys %$hash1) {
        # BUG: You wrote `$hash2{$a}`, but probably meant `$hash2->{$a}`.
        if (!exists $hash2->{$key}) {
            # BUG: I think you probably meant for the following
            # `$info1` reference to refer to the value inside
            # `$hash1->{a}` and not the last line from the prior
            # loop. Using lexical variables will detect these
            # types of problems.
            my $info1 = $hash1->{$key}{info1};
            print {$output} "$key\t$info1\n";
        }
    }
}

# If you use lexical file handles, calling close is not required. They
# get closed at the end of the containing scope, which in this case is
# the end of the script.

print "Done!\n";

暂无
暂无

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

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