[Israel.pm] hash

Gabor Szabo szabgab at gmail.com
Sun Sep 11 05:38:02 PDT 2011


On Sun, Sep 11, 2011 at 3:33 PM, Chanan Berler <bc.other at gmail.com> wrote:
> Hello All,
>
> I have a reference to hash structure, and a path where data is found.
> Is it possible to retrieve the data where path is found ?
>
>
> # What I am looking for:
> my $hash = {
>        'key1' => { 'key11' => 10 },
>        'key2' => { 'key21' => { 'key22' => { 'key23' => 100 }}},
>        'key3' => { 'key31' => { 'key321' => 20,
>                                                         'key322' => 200 }}
> };
>
> # this will not work since it's not referencing to nothing
> my $path = {'key3'}->{'key31'}->{'key321'};
>
> # doesn't work for me :-(
> print $hash->{$path};
>
> # work (but i wanna use $path as location reference)
> print $hash->{'key3'}->{'key31'}->{'key321'};

you can do this:

my $path = "{'key3'}->{'key31'}->{'key321'}";
print eval "\$hash->$path";

thought I find it very dangerous.

Why do you need this?

Gabor


More information about the Perl mailing list