On Mon, 2005-03-28 at 18:43, Uri Guttman wrote:
> >>>>> "ZL" == Zhuang Li <[EMAIL PROTECTED]> writes:
>
> ZL> Hi, given an array: @a = ('E1', 'E2', ..., 'En');
> ZL> Is there an easy way, hopefully one liner, to do the following without a
> ZL> loop? If not, will Perl support this in Perl 6?
>
>
> ZL> $hash->{E1}->{E2}->...->{En} = 1;
I'm not sure I see P6 as solving this problem (though it's just as easy
in P6 as P5). What you're asking for is:
@a = ('E1', 'E2','En');
$hash={};
setmultihashvalue($hash,[EMAIL PROTECTED],1);
sub setmultihashvalue {
my $ref = shift;
my $keys = shift;
my $value = shift;
if (@$keys > 1) {
my $key = shift @$keys;
if ($ref->{$keys->[0]}) {
$ref = $ref->{$key};
} else {
$ref = $ref->{$key} = {}; #autoviv
}
return setmultihashvalue($ref,$keys,$value);
} else {
$ref->{$keys->[0]} = $value;
}
}
use Data::Dumper;
print Dumper($hash);
--
Aaron Sherman <[EMAIL PROTECTED]>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback