CIS 33A Perl handout week 10 part 3

Preliminary material should be here.

Sample programs from Clare Nguyen (edited by Ira Oldham)

############## reference ############

Lecture week 10 hour 3 Example Part 1

#########################

1.	$var = 4;
2.	@arr = (1,2,3);
3.	%hash = (1,a,2,b,3,c);

4.	$varRef = \$var;		# points to a scalar
5.	print "$varRef\n";		# prints the address of $var
6.	print "$$varRef\n";		# prints 4, the value of $var

7.	$arrRef = \@arr;		# points to an array
8.	print "$arrRef\n";
9.	print "@$arrRef\n";	# uses @ to dereference to an array

10.	$hashRef = \%hash;		# points to a hash
11.	print "$hashRef\n";
12.	foreach (keys %$hashRef)  # uses % to dereference to hash
13.	{
# $_ takes on each value of a key
# The first $_ prints the key
# The $$hashRef{$_} prints the corresponding value
# $hashRef is the pointer to the hash
# $$hashRef dereferences the pointer
#   The first $ in the dereference says scalar value
#   It is a scalar because { } is used to get one entry
14.	print "$_ $$hashRef{$_}\n";

# Alternate code to do the same thing using -> operator
# The -> operator is commonly used
# OR print "$_ $hashRef->{$_}\n"; # -> dereference operator
15.	}

#########################

Lecture week 10 hour 3 Example Part 2

#########################

1.	$arrRef = [1,2,3,4];	# anonymous array
2.	print "$arrRef\n";		# prints address
3.	print "@$arrRef\n";		# prints 1 2 3 4

# Now we will construct an array containing the addresses of 
#    two arrays
4.	$arrRef2 = [10,20,30];
5.	$arrRef3 = [100,200,300];
6.	@arr = ($arrRef2,$arrRef3);	# contains two addresses
7.	foreach $ref (@arr)			# for each array address
8.	{
# dereference and print an array
9.	print "@$ref\n"
10.	}
# $arr[1] is the address of (100,200,300)
# Then use the -> operator to print the value 300
11.	print "$arr[1]->[2]\n";
# alternate notation, omitting the -> in between [1]->[2]
# It works the same way
12.	print "$arr[1][2]\n";

# This next example gets complicated
# $arrRef is a pointer to an anonymous array
# The first element in the array is the scalar number 1
# The second element in the array is the scalar number 2
# The third element in the array is a pointer to 
#    another array
# The following is how the third element is produced:
# [10, 20, 30] produces the address of an anonymous array 
#    containing ( 10, 20, 30 )
13.	$arrRef = [1,2,[10,20,30]];
14.	print "@$arrRef\n";		# prints 1 2 address of (10,20,30)
# next print the contents of (10, 20, 30)
15.	foreach (0..2)
16.	{
# $arrRef points to array containing: 
#    (1, 2, address of (10,20,30)

# $arrRef->[2] is the contents of the third element
# That is the address of (10,20,30)
# The second -> dereferences the address of (10,20,30)
#   to get an element from that array, using subscript $_
# The values of $_ are 0, 1, then 2
# So 10 20 30 is printed
17.	print "$arrRef->[2]->[$_]\n"; 
18.	}
# It is a little easier to understand if done in two steps:
# $ArrRef2 = $arrRef->[2];	# get address of (10,20,30)
# print "@$ArrRef2\n";		# print 10 20 30

#########################

Lecture week 10 hour 3 Example Part 3

#########################

1.	$hashRef = {1=>"a", 2=>"b", 3=>"c"}; # pointer to an 
							 #   anonymous hash
2.	foreach (keys %$hashRef)		 # dereference pointer
3.	{
4.	print "$_ $hashRef->{$_}\n";		# $_ is the key
# $hashRef->{$_} is the 
#    corresponding value
5.	}

# In the following hash the keys are 1 and 2
# Each corresponding value is the addresses of an anonymous 
#    array.
6.	%hash = (1=>["a","b","c"],2=>["x"]);

# $hash {1} provides the value corresponding to key 1
# The corresponding value is a pointer to array (a,b,c)
# That pointer is returned by the { } block of code
#    containing the line of code  $hash{1} ;
#	Note that the semicolon may be omitted before the 
#		closing brace
# The @ dereferences the pointer
# a b c prints
7.	print "@{$hash{1}}\n";		# prints a b c	

#########################

Lecture week 10 hour 3 Example Part 4

#########################

# % hash has keys 1 and 2
# Each corresponding value is a pointer to an hash
1.	$hashRef1 = {10,"a",20,"b"};
2.	$hashRef2 = {100,"x",200,"z"};
3.	%hash = (1=>$hashRef1,2=>$hashRef2);
4.	foreach $key (keys %hash)	# $key takes values 1 and 2
5.	{
# $hash{$key} is a value from hash; it is a pointer
# % dereferences the pointer
# $subkey takes values 10 and 20, next time 100 and 200
6.	foreach $subkey (keys %{$hash{$key}})
7.	{
# prints a b x z (one per line)
8.	print "$subkey $hash{$key}->{$subkey}\n";
9.	}
10.	}

# do it again using values function
11.	foreach $key (keys %hash)	# $key takes values 1 and 2
12.	{
# prints a b on one line, next time x z on the next line
13.	print values %{$hash{$key}},"\n";
14.	}