Thread In anonymes Hash umwandeln (9 answers)
Opened by J-jayz-Z at 2005-11-15 21:03

J-jayz-Z
 2005-11-15 22:45
#60188 #60188
User since
2005-04-13
625 Artikel
BenutzerIn
[Homepage] [default_avatar]
Dank dir erstmal für die Antwort. Aber ich bekomm den Wert von ->{x}->{y} durch ->{x}->{z} überschrieben, wieso ist mir aber nicht besonders klar...
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $bla = Container->new();
$bla->blub("foo/bar" => "eins");
$bla->blub("foo/bla" => "zwei");

my $foo = $bla->getValue();

print Dumper $foo;

package Container;

sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}

sub blub {
my $self = shift;
$self->{Hash} = {};
my %foo = @_;
my @daten;
foreach(keys %foo) {
my @split = split "/", $_;
push @daten, [ @split ];
}


my $tmp_key;

foreach my $data (@daten) {
if(ref $data eq 'ARRAY') {
$tmp_key = "";
my $count = 0;
foreach(@{ $data }) {
$tmp_key .= $_;
$tmp_key .= "/" unless($_ eq $data->[-1]);
$count++;
}
my $max = scalar(@{$data});
my $start = $self->{Hash};
for my $i (1 .. $max) {
my $key = $data->[$i-1];
my $val = $i == $max ? $foo{$tmp_key} : { };
$start = _prepareOrSet($start, $key, $val);
}
}
}
}
sub _prepareOrSet {
my ($href, $key, $val) = @_;
$href->{$key} = $val unless defined $href->{$key};
return $href->{$key};
}

sub getValue {
my $self = shift;
return $self->{Hash};
}
perl -Mstrict -Mwarnings -e 'package blub; sub new { bless {} } sub bar {my $self=shift; $self->{bla}="5065726c2d436f6d6d756e697479"; return $self->{bla};} my $foo=blub->new();print "Hallo ";print pack("H*",$foo->bar()); print "\n"'

http://perl-tutor.de

View full thread In anonymes Hash umwandeln