Thread Hash::Merge cut'n merge (8 answers)
Opened by lichtkind at 2004-01-01 19:07

esskar
 2004-01-02 03:19
#50555 #50555
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
hab ich das so richtig verstanden ?

Code: (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
#!/usr/bin/perl

use strict;
use warnings;

sub hash_update
{
    my ($x, $y) = @_;
    
    my $tx = ref $x;
    my $ty = ref $y;
    my $retval;
 
    die "Can not update due to different object type! ($tx != $ty)" unless $tx eq $ty;
 
    if($tx eq "ARRAY")
    {
        $retval = [];
        for my $i (0 .. _min(scalar(@{$x}), scalar(@{$y}))-1)
        {
            die "Array element was not expected!" unless ref $x->[$i];
            push @{$retval}, hash_update($x->[$i], $y->[$i]);
        }       ; 
    }
    elsif($tx eq "HASH")
    {
        $retval = {};
        for my $k (keys %{$x})
        {
            $retval->{$k} = exists $y->{$k} ? hash_update($y->{$k}, $x->{$k}) : $x;
        }
   }
   elsif($x ne "SCALAR")
   {
       $retval = $x;
   }

   return $retval;
}

sub _min
{
    return $_[0] < $_[1] ? $_[0] : $_[1];
}
\n\n

<!--EDIT|esskar|1104628817-->

View full thread Hash::Merge cut'n merge