#!perl
#Perl "Memory Structures" Frequently Asked Questions
# from the simple to the more complex.....
############### $string #######################
# a simple scalar variable is denoted with a $
# a single equal sign assigns the value on the right side
# to the variable on the left side.
$name = 'joe'; # the variable labelled 'name' contains the string 'joe'
$who = $name; # now, $who contains 'joe', just like $name
############### @arrays ########################
# an array (list) of string variables,(maybe names) is denoted with an @
@names = ('joe','jane','robert','phillip');
# refering to one element of an array
# note that we count from zero, not from one.
$who1 = $names[0];
$who2 = $names[1];
$who3 = $names[2];
$who4 = $names[3];
# the [3], refers to 'the fourth element of the
# array labelled, names'. Also, note the use of
# '$' in the array label because we are refering
# to one element of the array.
print "who1 - $who1\n";
print "who2 - $who2\n";
print "who3 - $who3\n";
print "who4 - $who4\n\n";
# changing an element in an array
@names = ('joe','jane','robert','phillip');
$names[2] = 'rob'; # robert prefers his nickname
print "Changed robert's name\n";
foreach (@names) { print "$_ \n"; }
# build an array from two others.
@names = '';
@names2 = ('joe','jane');
@names3 = ('robert','phillip');
@names = (@names2,@names3);
print "\nJoined 2 arrays into 1.\n";
foreach (@names) { print "$_ \n"; }
# push a new element onto an array
push @names,helen;
print "\nAdded helen to array\n";
foreach (@names) { print "$_ \n"; }
# remove/catch last element from array
$lastOne = pop(@names);
print "\n$lastOne is no longer part of the array.\n";
foreach (@names) { print "$_ \n"; }
# remove and catch the first element of an array
$firstOne = shift(@names);
print "\nFirst name was $firstOne, is now $names[0]\n";
foreach (@names) { print "$_ \n"; }
# put a new element on the front of the list
# where push adds the element to the end unshift puts the new
# element first.
unshift @names,'joe';
print "\nPut first element back in first slot.\n";
foreach (@names) { print "$_ \n"; }
# delete an element in an array
undef $names[2];
print "\nDeleted the third element\n";
foreach (@names) { print "$_ \n"; }
############### %hashes ########################
# a hash, or associative array, is simply a list of
# name/value pairs. A telephone book is a list of
# name/value pairs. For each key (name-address) there
# is a value (phone number).
%phone_numbers = (
'joe','573-1668',
'jane','573-1543',
'rob','123-4567',
'phillip','234-5678'
);
# print rob's number by refering to it with it's key (rob).
print "\nRob's number is -> $phone_numbers{'rob'}\n";
# Note that when refering to one value in a hash, you
# use a '$' on the front (after all, you are asking for a
# single value here, not the entire hash).
# Also, note the braces instead of the brackets that were
# used with the arrays.
# Change a value in a hash
# Rob's number is wrong, fix it
$phone_numbers{'rob'} = '234-4567';
print "\nFixed Rob's number -> $phone_numbers{'rob'}\n";
# to add a new name/value pair to a hash, simply set the value.
$phone_numbers{'mike'} = '987-6543';
# get all of the keys in a hash using the 'keys' function
@who = keys(%phone_numbers);
print "\nkeys in hash are -> @who\n";
# print phone numbers for everyone in the hash
foreach $who (keys(%phone_numbers))
{
print "NAME - $who\tNUMBER - $phone_numbers{$who}\n";
}