Alexandre Checinski am Donnerstag, 24. November 2005 12.11:
> Hello everobody,
> I'm new to this list.
>
> I have a problem to solve that have been mesing with for quite a few
> days now.
>
> I have lists that looks like this :
> (A,B,C,1,5)
> (A,B,C,1,2)
> (A,B,2,1,2)
> (A,B,C,D,1)
> (A,B,Y)
>
> And I would like to get something like this :
> {
> A => {
> B => {
> C => {
> 1 => {
> 5 => 1,
> 2 => 1
> }
> },
> D => {
> 1 => 1
> }
> 2 =>{
> 1 => {
> 2 => 1
> }
> }
> },
> Y => 1
Typo? Y should be on the same level as C i think.
> }
> }
>
> Has anyone any idea on how to do it?
>
> Thx for your help
Ok, no idea what you want to do with this structure, but:
Here is a way. I think it is not the most elegant and, due to the eval $string
not unproblematic (you have to have the control over every data used within
in), and certainly not the most performant...
I choosed the eval way because of the different size of the lists.
I'm interested to see better solutions :-)
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my @lists=(
[qw(A B C 1 5)],
[qw(A B C 1 2)],
[qw(A B 2 1 2)],
[qw(A B C D 1)],
[qw(A B Y)],
);
my $struct;
for my $l (@lists) {
no strict 'vars';
my $s='$struct->'.(join '', map { '{'.$l->[$_].'}' } [EMAIL PROTECTED]).'=1;';
eval $s or die;
}
warn Data::Dumper::Dumper ($struct);
$VAR1 = {
'A' => {
'B' => {
'C' => {
'1' => {
'2' => 1,
'5' => 1
},
'D' => {
'1' => 1
}
},
'Y' => 1,
'2' => {
'1' => {
'2' => 1
}
}
}
}
};
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>