A regular (scalar) variable |
$a = 5; $xyz="1234"; |
An (regular) array |
@a = ( 123, "abc", 456 ); |
The second element of the array ( a scalar, indexes start at 0 ) |
$a[1]; |
Accessing all elements of an array |
foreach $item ( @a ) { print "$item\n" } |
A hash array. Why do we have to index items by a number? Can't we do it with a string instead? Thats what a hash is for! |
%myHash = ( bananas => "yellow", apples => "red", ); |
Accessing an item in the hash |
print "The color of bananas is: ",$myHash{'bananas'},"\n"; |
Accessing all elements of an hash |
foreach $fruit ( keys %myHash ) { print "$fruit is the color $myHash{'$fruit'}\n" } |
REFERENCES ( a.k.a. Pointers ) Note:All references are scalar variables. |
A reference to a scalar variable. |
$aRef = \$a; |
Printing a reference to a scalar. The {} are optional. |
print "${$a}\n"; |
A reference to an array |
$aRef = \@a; |
De-Referencing the entire array. |
@aCopy = @{$aRef}; |
Printing an element from a reference to an array. |
print "$aRef->[1]\n"; |
Creating a reference to an array. Note the change from our previous example. @a is now $a and the ('s are changed to ['s |
$a = [ 123, "abc", 456 ]; |
Accessing all elements from an referece to an array |
foreach $item ( @{$a} ) { print "$item\n" } |
A reference to a hash |
$myRef = \%myHash; |
Accessing all elements of an hash via a reference. |
foreach $fruit ( keys %{$myRef} ) { print "$fruit is the color $myRef->{'$fruit'}\n" } |
A hash array reference. Note the change from our previous example. %myHash is now $myRef and the ('s are changed to {'s |
$myRef = { bananas => "yellow", apples => "red", }; |
How to use quoted text in Parse::RecDescent
From: