如何使用Perl比较两组哈希引用?
所以我有两个相似信息的文件 例子 test.txt如何使用Perl比较两组哈希引用?,perl,hash,Perl,Hash,所以我有两个相似信息的文件 例子 test.txt 1=1 2=2 3=3 test_2.txt 1=1 2=4 3=5 4=4 5=5 6=6 现在我想打印每个哈希键和值 my %hash1; my $scalar_value1; my $scalar_value2; my $file = "/test/test.txt"; open (TEST, "<$file") or die "$!"; while (TEST) { ($scalar
1=1
2=2
3=3 test_2.txt
1=1
2=4
3=5
4=4
5=5
6=6 现在我想打印每个哈希键和值
my %hash1;
my $scalar_value1;
my $scalar_value2;
my $file = "/test/test.txt";
open (TEST, "<$file") or die "$!";
while (TEST) {
($scalar_value1, $scalar_value2) = split( '=' );
$hash1{$scalar_value1}{'value1'} = $scalar_value1;
$hash1{$scalar_value1}{'value2'} = $scalar_value2;
}
close TEST;
foreach my $scalar_value1 (sort keys %hash1) {
print "$hash1{$scalar_value1}{'value1'} | $hash1{$scalar_value1}{'value2'}";
}
my %hash2;
my $scalar_value_1_2;
my $scalar_value_2_2;
my $file_2 = "/test/test2.txt";
open (TEST_2, "<$file_2") or die "$!";
while (TEST_2) {
($scalar_value_1_2, $scalar_value_2_2) = split( '=' );
$hash1{$scalar_value_1_2}{'value_1_2'} = $scalar_value_1_2;
$hash1{$scalar_value_1_2}{'value_2_2'} = $scalar_value_2_2;
}
close TEST_2;
foreach my $scalar_value_1_2 (sort keys %hash1) {
print "$hash1{$scalar_value_1_2}{'value1_2'} | $hash1{$scalar_value_1_2}{'value1_2'}";
}
为了更清晰,我重写了你的程序。“chomp”很重要,包括删除新行
use strict;
use warnings;
sub read_hash {
my $fname = shift;
open (my $fh, "<",$fname) or die "$!";
my %hash;
while (<$fh>) {
chomp;
my ($key,$value)=split /=/;
$hash{$key}=$value;
}
return %hash;
}
my %hash1=read_hash("test.txt");
foreach my $key (sort keys %hash1) {
print "$key = $hash1{$key}\n";
}
my %hash2=read_hash("test1.txt");
foreach my $key (sort keys %hash2) {
print "$key = $hash2{$key}\n";
}
#------------------------
foreach my $key (sort keys %hash2) {
if (exists $hash1{$key}) {
print "$key exists in first hash\n";
} else {
print "$key does not exist in first hash\n";
}
}
使用严格;
使用警告;
子读取散列{
我的$fname=班次;
open(my$fh,“听起来像是一个简单的集合交叉问题
use strict;
use warnings;
sub read_hash {
my $fname = shift;
open (my $fh, "<",$fname) or die "$!";
my %hash;
while (<$fh>) {
chomp;
my ($key,$value)=split /=/;
$hash{$key}=$value;
}
# let's retrun the reference here. With big hashes, you want to avoid copying
return \%hash;
}
my $h1=read_hash("test.txt");
my $h2=read_hash("test1.txt");
map {print "$_ = $h1->{$_}\n"} sort keys %$h1;
map {print "$_ = $h2->{$_}\n"} sort keys %$h2;
map {print "key from h1 $_ exists in h2\n" if exists $h2->{$_} } sort keys %$h1;
# if you just want to take out the items from $h2 that also exists in $h1.
my %h3 = map {$_=>$h2->{$_} if exists $h2->{$_}} keys %$h1;
使用严格;
使用警告;
子读取散列{
我的$fname=班次;
打开(my$fh,“{$}\n”}排序键%$h1;
映射{print“$\=$h2->{$\}\n”}排序键%$h2;
映射{print“key from h1$\存在于h2\n”如果存在$h2->{$\排序键%$h1;
#如果您只想从$h2中取出同样存在于$h1中的项目。
我的%h3=映射{$\=>$h2->{$\}如果存在$h2->{$\}键%$h1;
请注意,该程序使用map to do list迭代。map的最后一次使用创建了一个哈希,其中包含两个哈希的键的交集。为了进一步优化代码,我将遍历键数较少的哈希。还请注意,上面的方法不会检查键的值。它只比较钥匙本身
希望这能有所帮助。看看侧边栏中的所有“相关”链接——在最上面的几个链接中也有类似的算法。如果第一个散列引用只记住第一个散列中的最后一个键和值,我该怎么办?卢克:你的文件中有“key=value”这样的行“在同一文件中重复?是的,可以在文件中重复键值。始终在while(){…}之前重复local$\u;
”
在子例程中。如果没有本地,这将是一个等待出现的错误luke:您需要创建对数组的引用作为散列值并推送到它。为什么要使用map进行打印?这里的foreach更好。这是真的。foreach更适合打印。感谢您的提示。
use strict;
use warnings;
sub read_hash {
my $fname = shift;
open (my $fh, "<",$fname) or die "$!";
my %hash;
while (<$fh>) {
chomp;
my ($key,$value)=split /=/;
$hash{$key}=$value;
}
# let's retrun the reference here. With big hashes, you want to avoid copying
return \%hash;
}
my $h1=read_hash("test.txt");
my $h2=read_hash("test1.txt");
map {print "$_ = $h1->{$_}\n"} sort keys %$h1;
map {print "$_ = $h2->{$_}\n"} sort keys %$h2;
map {print "key from h1 $_ exists in h2\n" if exists $h2->{$_} } sort keys %$h1;
# if you just want to take out the items from $h2 that also exists in $h1.
my %h3 = map {$_=>$h2->{$_} if exists $h2->{$_}} keys %$h1;