Hello Ritz@Perl, and welcome to the Monastery!

First, please put your code in <code> ... </code> tags to make it easier to read.

Now to your question. Consider the following, which shows your code in an example context:

#! perl use strict; use warnings; my %hXCR = ( X => { GD => { 0 => { SKT => 17 } } } ); my %hRCX1 = ( X => { GD => { '*' => { SKT => '' } } } ); my $M = 'X'; my $s = join ( ";", $hXCR{$M}{GD}{0}{SKT} || 0, $hRCX1{$M}->{GD}->{'*'}->{SKT} || 0, ); print "\n\$s = |$s|\n";

Output:

17:41 >perl 1026_SoPW.pl $s = |17;0| 17:41 >

Yes, $hXCR is a hash with the value of $M as one of its keys. But the value is not GD, it is a reference; specifically, a reference to an anonymous hash which has GD as one of its keys. The value corresponding to GD is another reference to another anonymous hash, this time with 0 as a key and a third reference to a third anonymous hash as its corresponding value. The third anonymous hash contains a key SKT and a corresponding value; so the whole expression:

$hXCR{$M}{GD}{0}{SKT}

evaluates to that value (which is 17 in my example).

The expression || 0 means: if the value is “true” (according to Perl’s definition of truth), then the expression evaluates to the value on the left-hand side; but if it’s “false”, then the whole expression evaluates to 0. For example, in my script the second expression is the empty string, which in Perl is false, so the expression becomes 0 as you can see in the output.

Arrows are used to de-reference variables containing references, but in this case they are not needed. To get an understanding of all this, please read perlreftut, perldsc, and perllol.

Updates: (1) Improved wording slightly.

(2) This is what helped me to understand Perl’s complex data structures: In Perl, there are three kinds of variables: scalar, array, and hash. The first is singular, the second and third are plural. But the elements contained by an array or hash can only be scalars. To get an array of arrays (say), you need a way of representing an array (the inner one) as a scalar. That’s where references come in: take a reference to an array, and you have a scalar which can be (i) dereferenced to access the array, but also (ii) itself stored as an element in an array (the outer one).

(3) For Perl’s definition of “true” and “false”, see perlsyn#Truth-and-Falsehood.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: query on hash understanding by Athanasius
in thread query on hash understanding by Ritz@Perl

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.