Maelstrom has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to extend this to cover any level of nesting so that %a.deeply.nested.value would return $var{'a'}{'deeply'}{'nested'}{'value'} and the only thing I've come up with is a function that splits on the '.' and loops through the array to build a statement I can eval but I'm sure there has to be a more elegant way. If possible I'd like to avoid lookbehinds/lookaheads to keep the code portable to languages that don't support that. I'm also thinking I should I should be using $var{'foo'} instead of $var{'default'}{'foo'} so feel free to leave that out of any suggested solutions.$work =~ s{%(\w+)(?:\.(\w+))?}{ defined $2 ? $var{$1}{$2} : $var{default}{$1} }eg;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Recursively substitute against multidimensional hash
by haukex (Archbishop) on Feb 14, 2025 at 07:50 UTC | |
by Maelstrom (Beadle) on Feb 14, 2025 at 10:16 UTC | |
by Maelstrom (Beadle) on Feb 14, 2025 at 18:32 UTC | |
by haukex (Archbishop) on Feb 15, 2025 at 10:22 UTC | |
Re: Recursively substitute against multidimensional hash
by Fletch (Bishop) on Feb 14, 2025 at 15:20 UTC | |
by Maelstrom (Beadle) on Feb 14, 2025 at 18:36 UTC |