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

pq
 2005-11-15 22:45
#60187 #60187
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=J-jayz-Z,15.11.2005, 20:03]
Code: (dl )
my $foo = &splitme("foo/lol" => "blub","foo/lol/bla" => "grml");

Kann mir da jemand helfen ?[/quote]
kannst du bitte mal sagen, was da herauskommen soll?
einmal soll $foo->{foo}->{lol} = "blub" sein, dann wiederum
soll aber $foo->{foo}->{lol}->{bla} = "grml" sein.
das geht nicht.
falls du dich da nur vertippt hast:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sub splitme {
   my (%pairs) = @_;
   my $hash = {};
   for my $keypath (keys %pairs) {
       my @paths = split m#/#, $keypath;
       my $value = $pairs{$keypath};
       make_hash($hash, $value, @paths);
   }
   return $hash;
}

sub make_hash {
   my ($hash, $value, @paths) = @_;        
   my $ref = \$hash;                        
   for (@paths) {
       $ref = \$$ref->{$_};
   }
   $$ref = $value;
}
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread In anonymes Hash umwandeln