And there's another favorite:
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $hash = {}; while( my $line = <DATA> ) { chomp($line); $line =~ m/(\s*)(.*)/; my $level = int(length($1) / 2); my $caption = $2; my $node = { label => $caption, children => [] }; if( $level == 0 ) { $hash = $node; } else { my $hash_item = $hash; for (1 .. ($level - 1)) { $hash_item = @{$hash_item->{children}}[scalar(@{$hash_item +->{children}}) - 1]; } push(@{$hash_item->{children}}, $node); } } print Dumper ($hash); __DATA__ Building House Window Glas Silicium Door Roof Wood Hut Pizza Garage Door
In reply to Re: tree in hash
by relax99
in thread tree in hash
by Murcia
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |