使用perl对哈希值求和哈希

使用perl对哈希值求和哈希,perl,excel,hash,sum,Perl,Excel,Hash,Sum,我有一个Perl脚本,它解析Excel文件并执行以下操作:它计算a列中的每个值,计算B列中的元素数,脚本如下所示: use strict; use warnings; use Spreadsheet::XLSX; use Data::Dumper; use List::Util qw( sum ); my $col1 = 0; my %hash; my $excel = Spreadsheet::XLSX->new('inout_chartdata_ronald.xlsx'); m

我有一个Perl脚本,它解析Excel文件并执行以下操作:它计算a列中的每个值,计算B列中的元素数,脚本如下所示:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;
use List::Util qw( sum );

my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('inout_chartdata_ronald.xlsx');


my $sheet = ${ $excel->{Worksheet} }[0];


$sheet->{MaxRow} ||= $sheet->{MinRow};
my $count = 0;
# Iterate through each row
foreach my $row ( $sheet->{MinRow}+1 .. $sheet->{MaxRow} ) {

# The cell in column 1
my $cell = $sheet->{Cells}[$row][$col1];

if ($cell) {

    # The adjacent cell in column 2
    my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];  
    # Use a hash of hashes

    $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;

}
}
print "\n", Dumper \%hash;
$VAR1 = {
      '13' => {
                'klm' => 1,
                'hij' => 2,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
$VAR1 = {
      '13' => {
                'somename' => 3,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
输出如下所示:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;
use List::Util qw( sum );

my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('inout_chartdata_ronald.xlsx');


my $sheet = ${ $excel->{Worksheet} }[0];


$sheet->{MaxRow} ||= $sheet->{MinRow};
my $count = 0;
# Iterate through each row
foreach my $row ( $sheet->{MinRow}+1 .. $sheet->{MaxRow} ) {

# The cell in column 1
my $cell = $sheet->{Cells}[$row][$col1];

if ($cell) {

    # The adjacent cell in column 2
    my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];  
    # Use a hash of hashes

    $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;

}
}
print "\n", Dumper \%hash;
$VAR1 = {
      '13' => {
                'klm' => 1,
                'hij' => 2,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
$VAR1 = {
      '13' => {
                'somename' => 3,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
这很好,我的问题是:如何访问这个输出$VAR1的元素,以便执行:对于值13,klm+hij=3,并获得如下最终输出:

use strict;
use warnings;
use Spreadsheet::XLSX;
use Data::Dumper;
use List::Util qw( sum );

my $col1 = 0;
my %hash;

my $excel = Spreadsheet::XLSX->new('inout_chartdata_ronald.xlsx');


my $sheet = ${ $excel->{Worksheet} }[0];


$sheet->{MaxRow} ||= $sheet->{MinRow};
my $count = 0;
# Iterate through each row
foreach my $row ( $sheet->{MinRow}+1 .. $sheet->{MaxRow} ) {

# The cell in column 1
my $cell = $sheet->{Cells}[$row][$col1];

if ($cell) {

    # The adjacent cell in column 2
    my $adjacentCell = $sheet->{Cells}[$row][ $col1 + 1 ];  
    # Use a hash of hashes

    $hash{ $cell->{Val} }{ $adjacentCell->{Val} }++;

}
}
print "\n", Dumper \%hash;
$VAR1 = {
      '13' => {
                'klm' => 1,
                'hij' => 2,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
$VAR1 = {
      '13' => {
                'somename' => 3,
                'lkm' => 4,
              },
      '12' => {
                'abc' => 2,
                'efg' => 2
              }
    };
所以基本上我想做的是循环我的散列的最后一个散列,根据一个唯一的键访问它的特定元素,最后求和

任何帮助都将不胜感激。
谢谢

我使用了
@do_sum
来表示您想要做什么更改。新密钥在脚本中硬编码。请注意,如果子灰分中不存在任何键(
$found
标志),则不会创建新键


听起来你需要了解,也许哪种方法才是处理推荐信的好方法

如您所知,Perl有三种基本数据结构:

  • 标量(
    $foo
  • 数组(
    @foo
  • 散列(
    %foo
问题是这些数据结构只能包含标量数据。也就是说,数组中的每个元素都可以保存单个值,或者散列中的每个键都可以保存单个值

在您的例子中,
%hash
是一个散列,其中散列中的每个条目引用另一个散列。例如:

您的
%hash
中有一个项,其键为
13
。它不包含标量值,而是对另一个散列的引用,其中包含三个键:
klm
hij
lkm
。您可以通过以下语法引用:

${ hash{13} }{klm} = 1
${ hash{13} }{hij} = 2
${ hash{13} }{lkm} = 4
花括号可能是必要的,也可能不是。但是,
%{hash{13}
引用了
$hash{13}
中包含的散列,因此我现在可以引用该散列的键。你可以想象,当你谈论数组的散列时,情况会变得更加复杂。幸运的是,Perl包含了一个更简单的语法:

$hash{13}->{klm} = 1
%hash{13}->{hij} = 2
%hash{13}->{lkm} = 4

阅读有关散列以及如何操作它们的内容。在您熟悉了这一点之后,您可以开始学习面向对象的Perl,它以更安全的方式处理引用。

您希望使用什么键来进行求和?所有的钥匙?指定的密钥子集?是,此总和将影响所有密钥,13个密钥具有klm+hij,12个密钥也具有klm+hij。对不起,我举的例子很糟糕。在更现实的场景中,我有:$VAR1={'13'=>{'somename'=>3,'lkm'=>4,},'12'=>{'somename'=>9,'lkm'=>6};谢谢你的帮助,但我想为我散列中的所有密钥执行此操作。我想需要一个回路吧?(我真的刚刚开始学习Perl,感谢您的理解:))谢谢您,这非常有帮助。还有一个问题,我想用其他键求和,就像
@do_sum
,我想
@do_sum2
有例如“abc”和“efg”,我的输出应该是这样的:'somename'=>10,'othername'=>12。我需要循环浏览@do_sum和
@do_sum2
,我该怎么做呢?@user1734229:现在应该很简单了。阅读一些文档并尝试找到解决方案。我找到了方法。谢谢!!:)