On Sun, Apr 13, 2008 at 4:27 PM, Kelly Jones
<[EMAIL PROTECTED]> wrote:
> I have a file with this in it:
>
> a.b = 10
> c.d.e = 11
> f.g.h.i.j.k = 12
>
> Based on that, I want to set:
>
> $HASH{a}{b} = 10;
> $HASH{c}{d}{e} = 11;
> $HASH{f}{g}{h}{i}{j}{k} = 12;
>
> This is easy using eval(), but is there a better way?
>
> I realize that entries like "a.b = 10" and "a.b.c = 13" would
> conflict, since setting $HASH{a}{b}{c} to 13 automatically sets
> $HASH{a}{b} to a hashref. I've confirmed my file has no such conflicts.
snip
Something like this should work (warning untested):
my ($path, $val) = $line =~ /^(\S+)\s*=\s*(.*)$/;
my @keys = split /[.]/, $path;
my $ref = %HASH;
$ref = $ref->{$_} = {} for @keys[0 .. $#keys - 1];
$ref->{$keys[-1]} = $val;
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/