1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/perl use strict; use Data::Dumper; my @keys = ('level1_1', 'level2_1', 'level3_1'); my %hash = (); $hash{data}{@keys} = 'test'; print "Dumper: '".Dumper(\%hash)."'<br>\n";
$hash{data}{level1_1}{level2_1}{level3_1} = 'test'
$hash{data}{level1_2} = 'test alt'
$hash{data}{join('#', @keys)} = 'test';
1 2 3 4 5 6 7 8 9 10 11 12 13 14
my @keys = ('level1_1', 'level2_1', 'level3_1'); my %hash = (); $hash{data} = {}; my $hashref = $hash{data}; for my $i (0..$#keys) { $hashref->{$keys[$i]} = ($i == $#keys ? 'test' : {}); $hashref = $hashref->{$keys[$i]}; } print "Dumper: '".Dumper(\%hash)."'<br>\n";