如何合并两个hash,键和相应的值都要合并
比如
%basket1 = (
"Apple" => "red",
"Banana" => "yellow",
"Lemon" => "yellow",
"Carrot" => "orange"
);
%basket2=(
"Apple" => "yellow",
"tomato"=>"red"
);
合并后的结果
%together =(
"Apple" => "red,yellow",
"Banana" => "yellow",
"Lemon" => "yellow",
"Carrot" => "orange",
"tomato"=>"red"
);
%basket1 = (
"Apple" => "red",
"Banana" => "yellow",
"Lemon" => "yellow",
"Carrot" => "orange"
);
%basket2=(
"Apple" => "yellow",
"tomato"=>"red"
);
合并后的结果
%together =(
"Apple" => "red,yellow",
"Banana" => "yellow",
"Lemon" => "yellow",
"Carrot" => "orange",
"tomato"=>"red"
);
作者: yakczh 发布时间: 2011-06-05
- my %basket1 = (
- "Apple" => "red",
- "Banana" => "yellow",
- "Lemon" => "yellow",
- "Carrot" => "orange"
- );
-
- my %basket2=(
- "Apple" => "yellow",
- "tomato"=>"red"
- );
-
- my %together=%basket1;
-
- foreach my $k (keys %basket2) {
- (exists $together{$k})?($together{$k}.=','.$basket2{$k}):($together{$k}=$basket2{$k});
- }
作者: x9x9 发布时间: 2011-06-05