Arrays 2个哈希数组之间的算术运算
我有一个带有YAML的散列数组,它取自linux命令。我有另一个散列数组,它具有相同的YAML集,但具有不同的值,由同一个命令执行,该命令在稍后运行 例如 及 现在,我在寻找两个值之间的数学运算。我正在寻找一个输出Arrays 2个哈希数组之间的算术运算,arrays,perl,hash,hashmap,perl-data-structures,Arrays,Perl,Hash,Hashmap,Perl Data Structures,我有一个带有YAML的散列数组,它取自linux命令。我有另一个散列数组,它具有相同的YAML集,但具有不同的值,由同一个命令执行,该命令在稍后运行 例如 及 现在,我在寻找两个值之间的数学运算。我正在寻找一个输出 content.none: bytes: 4 content.image.gif: bytes: 0 这是散列中相应值之间的数学运算结果 我尝试了迭代和执行diff,但它看起来像是一个累积输出 %host_stat_new=New stats %host_stat
content.none:
bytes: 4
content.image.gif:
bytes: 0
这是散列中相应值之间的数学运算结果
我尝试了迭代和执行diff,但它看起来像是一个累积输出
%host_stat_new=New stats
%host_stat_old=Old stats (taken from stats.yml, which contains old data)
while (true){
open my $FH1, ">stats.yml" or die "cannot write to stat file because $!\n";
foreach my $line4 (keys %host_stat_new){
print $FH1 "$line4:\n";
foreach my $line5 (keys %{$host_stat_new{$line4}}) {
print $FH1 " $line5: \n";
foreach my $line6 (keys %{$host_stat_new{$line4}{$line5}}){
my $diff1 = $host_stat_new{$line4}{$line5}{$line6};
my $diff2 = $host_stat_old{$line4}{$line5}{$line6};
$math=$diff1-$diff2;
#$host_stat1{$line4}{$line5}{$line6} = $math;
$host_stat_new{$line4}{$line5}{$line6} = $math;
print $FH1 " $line6: $host_stat_new{$line4}{$line5}{$line6}\n";
}
}
}
}
我做错了什么?是否有任何模块可以做到这一点?只需使用一些好的ole递归即可获得差异。此函数假设数据结构相同,可以添加功能以确认:
use YAML;
use strict;
use warnings;
my $str1 = <<YAML_STRING1;
content.none:
bytes: 31046
content.image.gif:
bytes: 0
YAML_STRING1
my $str2 = <<YAML_STRING2;
content.none:
bytes: 31050
content.image.gif:
bytes: 0
YAML_STRING2
my $data1 = Load($str1);
my $data2 = Load($str2);
my $diff = diff_recurse($data2, $data1);
print Dump($diff);
sub diff_recurse {
my ($str1, $str2) = @_;
my $diff;
if ('HASH' eq ref $str1) {
$diff = {};
for my $key (keys %$str1) {
$diff->{$key} = diff_recurse($str1->{$key}, $str2->{$key});
}
} elsif ('ARRAY' eq ref $str1) {
$diff = [];
for my $i (0..$#$str1) {
$diff->[$i] = diff_recurse($str1->[$i], $str2->[$i]);
}
} elsif (my $unknown = ref $str1) {
die "Unknown type $unknown\n";
} elsif ($str1 =~ /^[\d\.]+$/) {
$diff = $str1 - $str2;
} else {
$diff = $str1;
}
return $diff;
}
发生了什么?预期的输出是什么,给出的输出是什么?您可以在
%host\u stat\u old
和new
上使用Data::Dumper,这样我们就可以看到散列是如何填充的了吗?您是否使用strict
和使用warnings
?第一次的输出是diff,但是从第二次迭代开始,它是累积的。是的,使用了严格和警告。有道理。谢谢你,米勒。
%host_stat_new=New stats
%host_stat_old=Old stats (taken from stats.yml, which contains old data)
while (true){
open my $FH1, ">stats.yml" or die "cannot write to stat file because $!\n";
foreach my $line4 (keys %host_stat_new){
print $FH1 "$line4:\n";
foreach my $line5 (keys %{$host_stat_new{$line4}}) {
print $FH1 " $line5: \n";
foreach my $line6 (keys %{$host_stat_new{$line4}{$line5}}){
my $diff1 = $host_stat_new{$line4}{$line5}{$line6};
my $diff2 = $host_stat_old{$line4}{$line5}{$line6};
$math=$diff1-$diff2;
#$host_stat1{$line4}{$line5}{$line6} = $math;
$host_stat_new{$line4}{$line5}{$line6} = $math;
print $FH1 " $line6: $host_stat_new{$line4}{$line5}{$line6}\n";
}
}
}
}
use YAML;
use strict;
use warnings;
my $str1 = <<YAML_STRING1;
content.none:
bytes: 31046
content.image.gif:
bytes: 0
YAML_STRING1
my $str2 = <<YAML_STRING2;
content.none:
bytes: 31050
content.image.gif:
bytes: 0
YAML_STRING2
my $data1 = Load($str1);
my $data2 = Load($str2);
my $diff = diff_recurse($data2, $data1);
print Dump($diff);
sub diff_recurse {
my ($str1, $str2) = @_;
my $diff;
if ('HASH' eq ref $str1) {
$diff = {};
for my $key (keys %$str1) {
$diff->{$key} = diff_recurse($str1->{$key}, $str2->{$key});
}
} elsif ('ARRAY' eq ref $str1) {
$diff = [];
for my $i (0..$#$str1) {
$diff->[$i] = diff_recurse($str1->[$i], $str2->[$i]);
}
} elsif (my $unknown = ref $str1) {
die "Unknown type $unknown\n";
} elsif ($str1 =~ /^[\d\.]+$/) {
$diff = $str1 - $str2;
} else {
$diff = $str1;
}
return $diff;
}
---
content.image.gif:
bytes: 0
content.none:
bytes: 4