Thread Array in Hash umwandeln (1 answers)
Opened by norman at 2005-11-02 10:34

esskar
 2005-11-02 11:34
#59551 #59551
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
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
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @daten = (
['hosen','jeans','modern'],
['hosen','shorts'],
['roecke','mini']
);

my $hash = {};

foreach my $data (@daten) {
if(ref $data eq 'ARRAY') {
my $max = scalar(@{$data});
my $start = $hash;
for my $i (1 .. $max) {
my $key = $data->[$i-1];
my $val = $i == $max ? 1 : {};
$start = prepareOrSet($start, $key, $val);
}
}
}

sub prepareOrSet {
my ($href, $key, $val) = @_;
$href->{$key} = $val unless defined $href->{$key};
return $href->{$key};
}

print Dumper $hash;

View full thread Array in Hash umwandeln