[英]Perl - Create Array of Hashes based on Hash filter
I have this hash that contains some information: 我有包含一些信息的此哈希:
my %hash = (
key_1 => {
year => 2000,
month => 02,
},
key_2 => {
year => 2000,
month => 02,
},
key_3 => {
year => 2000,
month => 03,
},
key_4 => {
year => 2000,
month => 05,
},
key_5 => {
year => 2000,
month => 01,
}
);
I wan't to create an array of hashes in which each of the array elements, lists every single hash key/value pairs that has the same year and month. 我不会创建一个哈希数组,其中每个数组元素都列出具有相同年份和月份的每个哈希键/值对。
So basically I want to create something like this: 所以基本上我想创建这样的东西:
$VAR1 = [
'key_1' => {
'month' => 2,
'year' => 2000
},
'key_2' => {
'month' => 2,
'year' => 2000
}
], [
'key_3' => {
'month' => 3,
'year' => 2000
}
], [
'key_4' => {
'month' => 3,
'year' => 2000
}
], [
'key_5' => {
'year' => 2000,
'month' => 1
}
];
The real question here is: How can I compare a hash key key value's to other key key value's and make a map out of it. 真正的问题是:如何将散列键键值与其他键键值进行比较,并从中映射。
Thank you for your time! 感谢您的时间! =)
=)
I'm getting a slightly different results - key_3 and key_4 should belong to the same group. 我得到的结果略有不同-key_3和key_4应该属于同一组。
my %by_year_and_month;
undef $by_year_and_month{ $hash{$_}{year} }{ $hash{$_}{month} }{$_}
for keys %hash;
my $result;
for my $year (keys %by_year_and_month) {
for my $month (keys %{ $by_year_and_month{$year} }) {
push @$result, [ map { $_ => { month => $month, year => $year } }
keys %{ $by_year_and_month{$year}{$month} } ];
}
}
my %hash = (
key_1 => {year => 2000, month => 02},
key_2 => {year => 2000, month => 02},
key_3 => {year => 2000, month => 03},
key_4 => {year => 2000, month => 03},
key_5 => {year => 2000, month => 01}
);
my @v=qw(year month); # keys compared
my @k=sort keys %hash;
my(@a,%i);
push@{$a[sub{$i{join$;,map$_[0]{$_},@v}//=@a}->($hash{$_})]},($_=>$hash{$_}) for@k;
use Data::Dumper; print Dumper(\@a);
You say you want the following, but it makes no sense. 您说想要以下内容,但这没有任何意义。
$VAR1 = [$k,$v,$k,$v...], [...], [...], ...;
The accepted answer assumed you meant you want the following: 接受的答案假设您想要以下内容:
$VAR1 = [ [$k,$v,$k,$v,...], [...], [...], ... ];
That can be obtained as follows: 可以这样获得:
sub group_id { sprintf "%04d-%02d", @{ $_[0] }{qw( year month )} }
my %grouped;
for my $k (keys(%hash)) {
my $v = $hash{$k};
push @{ $grouped{ group_id($v) } }, $k, $v;
}
my $VAR1 = [ map { $grouped{$_} } sort keys %grouped ];
But that's a very weird format. 但这是一种非常奇怪的格式。 The following would make far more sense:
以下将更有意义:
$VAR1 = [ { $k=>$v,$k=>$v,...}, {...}, {...}, ... ];
That can be obtained as follows: 可以这样获得:
sub group_id { sprintf "%04d-%02d", @{ $_[0] }{qw( year month )} }
my %grouped;
for my $k (keys(%hash)) {
my $v = $hash{$k};
$grouped{ group_id($v) }{$k} = $v;
}
my $VAR1 = [ map { $grouped{$_} } sort keys %grouped ];
In both cases, the key is to use a hash to group similar things. 在这两种情况下,关键是使用哈希将相似的事物分组。
In both cases, the resulting groups are ordered chronologically. 在这两种情况下,生成的组都按时间顺序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.