in reply to Test if a subhash in a referenced hash exists
Part of my confusion seems to boil down to the following:
All of you write my hash structures like this
and as I understand the two following notations are equivalentif (not exists $coordinates->{$group}{$id}{$stage}{"coords"}) {
All three work, they are dereferencing the hash reference $coordinates and provide the hash reference of the anonymous subhash that has at its top level the coordinate numbers as keys.if (not exists ${$coordinates}{$group}{$id}{$stage}{"coords"}) { if (not exists ${${${${$coordinates}{$group}}{$id}}{$stage}}{"coords"} +)
The following tests for the $coordinates-> notation also go through without error:
However, what about %{$$coordinates{$group}{$id}{$stage}{"coords"}}that is used inif (not defined $coordinates->{$group}{$id}{$stage}{"coords"}) { if (!($coordinates->{$group}{$id}{$stage}{"coords"})) {
I always thought this is equivalent and also would give the hash reference of the anonymous subhash? Butforeach my $coord_no (keys %{$$coordinates{$group}{$id}{$stage}{"coord +s"}}) {…}
Thus, what does it return? And why is what it returns defined and true, but does not exist:if (not exists %{$$coordinates{$group}{$id}{$stage}{"coords"}}) { exists argument is not a HASH or ARRAY element
both go through just fine.if (not defined %{$$coordinates{$group}{$id}{$stage}{"coords"}}) { if (!(%{$$coordinates{$group}{$id}{$stage}{"coords"}})) {
To make my confusion complete, tests of a similar hash give different results:
Mmh.if (not exists %{$$original_data{$group}{$id}{$stage}}) { # exists argument is not a HASH or ARRAY element if (not defined %{$$original_data{$group}{$id}{$stage}}) { # works if (!(%{$$original_data{$group}{$id}{$stage}})) { # Can't use an undefined value as a HASH reference
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Test if a subhash in a referenced hash exists
by wfsp (Abbot) on May 29, 2010 at 15:52 UTC | |
by Henri (Novice) on May 30, 2010 at 18:30 UTC |