1 2 3 4
my %NewHashArray; foreach my $key (keys %{$HashArray1}){ %NewHashArray{$key}=[$$$HashArray1{$key}[0],$$$HashArray1{$key}[1]],$$$HashArray2{$key}[1]; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use Data::Dumper;
my %hash1 = (a => [1, 2], b => ['k', 'm']);
my %hash2 = (a => [3, 4], b => ['o', 'p']);
my %result;
for (keys %hash1) {
$result{$_} = [@{$hash1{$_}}, @{$hash2{$_}}];
}
print Dumper \%result;
#oder mit Aenderung:
for (keys %hash1) {
push @{$hash1{$_}}, @{$hash2{$_}};
}
print Dumper \%hash1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
use strict; use Data::Dumper; my %result; my ($hash1, $hash2) = &generate_hash; delete ${$hash2}{2}; #print Dumper(%{$hash1}); #print Dumper(%{$hash2}); foreach my $key (keys %{$hash1}){ my @Array; foreach (0..3){push (@Array, ${${$hash1}{$key}}[$_])} if (${$hash2}{$key}){ foreach (2..3){push (@Array, ${${$hash2}{$key}}[$_])}; }else{ foreach (2..3){push (@Array, "")}; } # print Dumper(@Array); $result{$key}=\@Array }#end foreach Addieren print Dumper(%result); sub generate_hash(){ my %test1; my %test2; foreach (1 .. 3) { my $date = &generate_date; $test1{$_}=[$date, sprintf("%3.0f", $_), &number, &number]; $test2{$_}=[$date, sprintf("%3.0f", $_), &number, &number]; } return (\%test1,\%test2) } sub generate_date{ my ($year,$month,$day); do {$year = sprintf("%4.0f", rand(2011));}until($year>1900); $month = sprintf("%02d", rand(12)); $day = sprintf("%02d", rand(31)); return "$year-$month-$day"; } sub number { return sprintf("%4.0f", rand(4999)); }
1 2 3 4 5 6 7 8
for my $key (keys %$hash1) { my @Array; push (@Array, @{$hash1->{$key}}[0..3]); my $ref=$hash2->{$key}; push (@Array, $ref?@$ref[2,3]:('','')); $result{$key}=\@Array; }
1
2
3
4
Rate while1 foreach1 for1
while 136/s -- -7% -8%
foreach 147/s 8% -- -1%
for 149/s 9% 1% --
%foo = (%foo, %bar); # wichtig sind die runden Klammern!!!
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %foo = ( localtime => [localtime], stat => [stat $0], ); my %res = (%ENV, %foo); print Dumper \%res;