Thanks to all of you, your comments are really helpful to me. The background of my question is, that I am at a point where my scripts are getting more numerous and complex, while I am still just repeating what has been working in the past without necessarily understanding why. To be able to make a step ahead I dearly need to clarify some basic concepts. These seem obvious when I read about them, but get mangled when I try to apply them. I am just not comfortable with (de-) referencing complex data structures and testing for existence.

Part of my confusion seems to boil down to the following:
All of you write my hash structures like this

if (not exists $coordinates->{$group}{$id}{$stage}{"coords"}) {
and as I understand the two following notations are equivalent
if (not exists ${$coordinates}{$group}{$id}{$stage}{"coords"}) { if (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.

The following tests for the $coordinates-> notation also go through without error:

if (not defined $coordinates->{$group}{$id}{$stage}{"coords"}) { if (!($coordinates->{$group}{$id}{$stage}{"coords"})) {
However, what about %{$$coordinates{$group}{$id}{$stage}{"coords"}}that is used in
foreach my $coord_no (keys %{$$coordinates{$group}{$id}{$stage}{"coord +s"}}) {…}
I always thought this is equivalent and also would give the hash reference of the anonymous subhash? But
if (not exists %{$$coordinates{$group}{$id}{$stage}{"coords"}}) { exists argument is not a HASH or ARRAY element
Thus, what does it return? And why is what it returns defined and true, but does not exist:
if (not defined %{$$coordinates{$group}{$id}{$stage}{"coords"}}) { if (!(%{$$coordinates{$group}{$id}{$stage}{"coords"}})) {
both go through just fine.

To make my confusion complete, tests of a similar hash give different results:

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
Mmh.

In reply to Re: Test if a subhash in a referenced hash exists by Henri
in thread Test if a subhash in a referenced hash exists by Henri

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.