my %colorfigshash = ();
my $tempcnt = <DATA>;
while($tempcnt=~m/Placement of Figure (?:[^\{]*)\{([^\{\}]*)\} Page ([^\n]*)\n/sg)
{
$colorfigshash{$1} = $2;
}
use Data::Dumper;
print Dumper \%colorfigshash;
__DATA__
Placement of Figure \hbox {10.7} Page 216
Pages in Color: 216
Placement of Figure \hbox {10.7} Page 217
Pages in Color: 217
当前输出:
$VAR1 = {
'10.7' => '216'
};
预期输出
$VAR1 = {
'10.7' => '216-217'
};
如果键重复,我们如何将这些值与前一个值合并。 如果键与哈希表中存储的最后一个值重复。任何人都可以提供解决方法,我们将不胜感激。
最佳答案
我将使用数组的哈希值,并按照以下方式重写它:
use strict;
use warnings;
my %colorfigshash;
while(<DATA>) {
chomp;
next unless /^Placement/;
my ($placement) = /\{(\d+\.\d+)\}/;
my ($page) = /Page (\d+)/;
push @{$colorfigshash{$placement}}, $page;
}
for (keys %colorfigshash){
print "$_ ";
print join ('-', @{$colorfigshash{$_}}), "\n";
}
__DATA__
Placement of Figure \hbox {10.7} Page 216
Pages in Color: 216
Placement of Figure \hbox {10.7} Page 217
Pages in Color: 217
10.7 216-217
关于perl - 在散列键重复中,将值连接到 perl 脚本中的先前键中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38608284/