Thanks all for your suggestions!
I never used 'eval', but from what I've read, it seems like a good thing!
The recursive way of doing it has nothing wrong, but I was wondering if there were other ways - to learn new tricks :)
Thanks again! Maybe I can actually post the recursive way:
my @array_2D = blablabla
my ($tree, $tmp);
foreach my $row (@array){
$tmp = extend_MTree($row);
$tree = add($tmp, $tree);
}
$tree now contains the hash!
sub extend_MTree($)
{
my ($a) = @_;
my %H_tmp;
if( $#{$a} == -1)
{
return 1;
}
my $elmt = shift @$a;
$H_tmp{$elmt} = extend_MTree($a);
return \%H_tmp;
}
sub add()
{
my ($tmp, $res) = @_;
foreach my $key (keys %$tmp)
{
# this is a new path, add here and leave
if(!defined($res->{$key}))
{
$res->{$key} = $tmp->{$key};
return $res;
}
# this part of the path exists, recurse
$res->{$key} = add($tmp->{$key}, $res->{$key});
return $res;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.