in reply to Re: property file parsing
in thread property file parsing

No need for recursion if a simple loop suffices:
my ( $first, @subkeys ) = split /\./, $key; my $last = pop @subkeys; my $ref = $properties{$first} ||= {}; $ref = $ref->{$_} ||= {} for(@subkeys); $ref->{$last} = $value;

Replies are listed 'Best First'.
Re^3: property file parsing
by pKai (Priest) on Feb 03, 2006 at 22:45 UTC

    Yeah, _insert is even tail recursive, so making a loop from it is no big deal. But, believe it or not, those recursive solutions just come more easily to me.

    BTW, your snippet fails to overwrite previously set to "True" scalar values. There is no case of this in the OP data sample. But this kind of anomaly is clobbered the same way by OP's script and mine.
    So you can even argue, that your algorithm crashing on such data is a feature :)

      Good point! I would also consider it a feature. I'd say it would be a syntax error in the config file, and I'd throw an exception for that if it happened.
        You might raise your own message, since Can't use string ("1") as a HASH ref while "strict refs" in use... isn't very descriptive to the end user for a data driven error ;-)
Re^3: property file parsing
by mifflin (Curate) on Feb 03, 2006 at 22:29 UTC
    perfect! thankyou.