import-bot (20211) [Avatar] Offline
#1
[Originally posted by bertrich]

#!c:perlin -w
use strict;

# I was unable to get something to work with the example at the bottom of
# page 149. I wrote a program that tried to extend that example in a small
# way, and this is the program, which is named page149b_test_questions.pl.
#
# Here's what I think is going on in the following code chunk...
# The code chunk below creates a hash, employees. The employees hash has
# two keys, asmith and brich. These two keys are scalar variables, which are
# named memory locations. The asmith key contains a reference to the memory
# location for an anonymous hash that contains information about Anne Smith.
# The brich key contains a reference to the memory location for an anonymous
# hash that contains information about Betty Rich.
#
# I assume the following about the anonymous hash that contains information
# about Anne Smith. The name key is a scalar that contains the value 'Anne
# Smith'. The age key is a scalar that contains the value 35. The children key
# is a scalar that contains a reference to an anonymous array that includes
# the elements 'Amanda' as the first element and 'Amy' as the second element.
# The beer key is a scalar that contains a reference to an anonymous array
# that includes the elements 'Pale Ale' as the first element and 'Lager' as
# the second element.
#
# I assume the following about the anonymous hash that contains information
# about Betty Rich. The name key is a scalar that contains the value 'Betty
# Rich'. The age key is a scalar that contains the value 72. The children key
# is a scalar that contains a reference to an anonymous array that includes
# the elements 'Frank' as the first element and 'Bert' as the second element.
# The beer key is a scalar that contains a reference to an anonymous array
# that includes the elements 'Budweiser' as the first element and 'Miller'
# as the second element.

my %employees = ( asmith => { name => 'Anne Smith',
age => 35,
children => ['Amanda', 'Amy'],
beer => ['Pale Ale', 'Lager'],
},
brich => { name => 'Betty Rich',
age => 72,
children => ['Frank', 'Bert'],
beer => ['Budweiser', 'Miller'],
},
);

# The next line starts with @ because we are dereferencing the anonymous array
# referenced by the children key. At the bottom of page 144 of the book, it
# says, "A few different ways of dereferencing a reference exist to obtain a
# value from that reference. The first method is to simply prefix a variable
# symbol of the proper type (either @, %, or $) onto the reference variable."
# I assume that the following statement begins with the @ character because
# the children key references an array.
print "@{$employees{asmith}{children}}
"; # prints: Amanda Amy

# I was wondering how to access particular elements in the anonymous array
# referenced by the children key. For example, for the Anne Smith anonymous
# hash, I wanted to try to print out just the first element in the anonymous
# array referenced by the children key (the string 'Amanda'). Using the same
# logic as in the previous statement, I assume that because the first element
# in the anonymous array referenced by the children key is a scalar variable,
# the de-referencing statement needs to start with a $ character.
print "${$employees{asmith}{children}[0]}
";

# The above line generates this error message:
# Can't use string ("Amanda") as a SCALAR ref while "strict refs" in use at
# at page149b_test_questions.pl line 62

# My questions are:
#
# What is the proper syntax to reference just the string 'Amanda' in the above
# print statement?
#
# From my explanation above of the employees hash, can you tell what concepts I
# am misunderstanding?
#
# Thanks for whatever help you can provide!
import-bot (20211) [Avatar] Offline
#2
Re: Questions about program based on example at bottom of page 149
[Originally posted by jandrew]

Your description of the %employees structure is fine. Your attempt at
dereferencing 1 element is a little off (your [0] is in the wrong
place). Let's look at the whole array dereference portion first:

print "@{$employees{asmith}{children}}
"; # prints: Amanda Amy

Now in a more abstract manner:

@ { some reference to an array }

Ok, now let's start with an ordinary array and move up:

@array = (1,2,3);
print "@array
";
print "$array[0]
";

Now with an array ref:

$aref = [1,2,3];
print "@$aref
";
print "$$aref[0]
";

Those last two we could also write:

print "@{$aref}
";
print "${$aref}[0]
";

So, where you see $aref in those two, just put in whatever array ref
you wish to access:

@{ $aref } <-- whole thing
${ $aref }[0] <-- element zero

@{ $employees{asmith}{children} } <-- whole thing
${ $employees{asmith}{children} }[0] <-- element zero


Of course, I would more likely use the arrow syntax to access
that element: Everything to the left of the arrow resolves to
a reference, the arrow points to the thing to lookup in that
reference:

$employees{asmith}{children}->[0]

And because we don't need an arrow between any two subscripts
we can just use:

$employees{asmith}{children}[0]

I hope this helps.

regards,
andrew