Thread HashArray - Neues Anlegen aus existierendem (10 answers)
Opened by Lebewesen at 2012-05-21 14:57

Lebewesen
 2012-05-21 16:58
#158442 #158442
User since
2011-09-09
62 Artikel
BenutzerIn

user image
So, jetzt habe ich mal eine ausführlicheres Beispiel.

Kann man Zeile 10-20 Optimieren?

Code (perl): (dl )
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));
}

Last edited: 2012-05-21 16:59:43 +0200 (CEST)

View full thread HashArray - Neues Anlegen aus existierendem