in reply to Re^2: Test if a subhash in a referenced hash exists
in thread Test if a subhash in a referenced hash exists
Here a scalar is returned. $VAR1 I think is the hash reference of the subhash.print Dumper($coordinates->{"AC"}{"132"}{"0"}{"coords"}); $VAR1 = { '1' => { 'value' => '4411478.623', 'name' => 'Xgeo' }, '2' => { 'value' => '5953375.013', 'name' => 'Ygeo' } };
This returns the hash contents (keys and the references of the next level subhashes) as a list.print Dumper(%{$$coordinates{"BD"}{"132"}{"0"}{"coords"}}); $VAR1 = '1'; $VAR2 = { 'value' => '4411478.623', 'name' => 'Xgeo' }; $VAR3 = '2'; $VAR4 = { 'value' => '5953375.013', 'name' => 'Ygeo' };
From how exists and defined behave, I take it that exists requires a single element, while defined is able to handle lists, too. I have to check that. But I take it I should test for the subhash reference ($hasref->) as almut pointed out and don't test the hash list itself (%{$$hasref...}).
The refs quick ref page and the keys page were new to me, thanks for pointing me there. Most recently I have been looking at perlref, perlreftut, perlsub, perlvar, perldata and exits. Generally, most of the examples seem to deal with the topmost layer of a HoH or its final leaves layer ie. when you finally access a scalar. Examples for the middle of a HoH when you have to deal with subhashes and subarrays, however, are much rarer. Maybe it is just obvious, but to me it's not intuitive from the start. At the topmost layer you access the hash (reference) - results are from print
and you access the leaves$hashref # HASH(x183e2e8) %($hashref} # %{HASH(x183e2e8)} %$hashref # %{HASH(x183e2e8)}
In the middle you always need the extra dereference to first get at the subhash reference (a scalar) and then with it access the subhash:$$hashref{$key1}{$key2}{$key3} # = scalar value
The following notations don’t work:$$hashref{$key1}{$key2}{$key3} # HASH(0x18b9df8) $hashref->{$key1}{$key2}{$key3} # HASH(0x18b9df8) %($$hashref{$key1}{$key2}{$key3}} # 1HASH(0x18b9e58)2HASH(0x18b9e88)
%($hashref{$key1}{$key2}{$key3}} # Global symbol %hashref requires e +xplicit package name %$hashref{$key1}{$key2}{$key3} # syntax error
Your comments and suggestions really helped me to cut that knot and understand a bit better what I am doing. Thanks to all of you!
|
|---|