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,
|