is this better ?
my $hash;
my $path = "{key3}->{key31}->{key323}";
$hash = {
'key1' => { 'key11' => 10 },
'key2' => { 'key21' => { 'key22' => { 'key23' => 100 }}},
'key3' => { 'key31' => { 'key321' => 20,
'key322' => 200 }}
};
my $value = eval "\$hash->$path" ? sprintf eval "\$hash->$path" : undef;
if (defined($value))
{
print $value;
}
else
{
print "Not defined";
}
On Sun, Sep 11, 2011 at 3:53 PM, Chanan Berler <[email protected]> wrote:
> I am sending request to server, and receive a response in HASH structure.
> I have a method that prints it's key=value, but i need only specific
> fields to be printed.
>
> I created a simple_trace method that prints to log only specific
> fields, out of the hash
> and wanted to sends params>{data} = { key1 => $where_in_hash,
> key2 =>
> $where_in_hash2
> };
>
> and than look over the keys, and use the value to print key=value
>
> Chanan
>
> On Sun, Sep 11, 2011 at 3:38 PM, Gabor Szabo <[email protected]> wrote:
>> On Sun, Sep 11, 2011 at 3:33 PM, Chanan Berler <[email protected]> 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
>> _______________________________________________
>> Perl mailing list
>> [email protected]
>> http://mail.perl.org.il/mailman/listinfo/perl
>>
>
>
>
> --
> ===================
> ---- Chanan Berler ----
> ===================
>
--
===================
---- Chanan Berler ----
===================
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl