List Functions and Basic Hashes
#!/usr/bin/perl -w
################ List Function Demo ##################
@list = (1,"hello","3","goodbye");
print "List is: @list\n";
print "Highest index of list is: $#list\n";
print "Number of elements in list is: ",scalar(@list),"\n";
print "Last element of list is: ", pop(@list),"\n";
print "First element of list is: ", shift(@list),"\n";
unshift(@list,1,2);
print "List is: @list\n";
push(@list,4,5);
print "List is: @list\n";
##### Proof that negative indices start at list end!
@list = ("hello", "goodbye", "hola", "aloha", "sayonara");
print "$list[-1] $list[-2]\n"; ##### List elements are SCALARS!!!!
foreach $element (@list) ###### The correct way to print lists!!
{
print $element;
$element =~ s/a/X/g; ###### Changes the list elements!!
}
print "@list\n"; ###### Observe the changed list!!
############### Hash Manipulation Demo #####################
%music = ("Fur Elise" => "Beethoven",
"Art of the Fugue" => "Bach",
"Sonata for Two Pianos and Percussion" => "Bartok");
foreach $key (sort(keys(%music)))
{
print "$key\n";
}
foreach $value (values(%music)) { print "$value\n" } ##### Acceptable format!
print "Yippee\n" if (exists($music{'FUR ELISE'}));
print "Hooray\n" if (exists($music{'Fur Elise'}));
print "$piece: $composer\n" while ($piece,$composer) = each(%music);
delete $music{"Fur Elise"}; ###### Removes Key/Value Pair!!
print "$piece: $composer\n" while ($piece,$composer) = each(%music);
######################### Output Below ########################
List is: 1 hello 3 goodbye
Highest index of list is: 3
Number of elements in list is: 4
Last element of list is: goodbye
First element of list is: 1
List is: 1 2 hello 3
List is: 1 2 hello 3 4 5
sayonara aloha ##### $list[-1] and $list[-2]
hello ##### List elements before substitution.
goodbye
hola
aloha
sayonara
hello goodbye holX XlohX sXyonXrX ##### List elements AFTER substitution
Art of the Fugue ###### Listing of sorted keys.
Fur Elise
Sonata for Two Pianos and Percussion
Bach ##### Values of the hash -- notice lack of sorted order!!
Bartok
Beethoven
Hooray ##### Proof that an "exists" is true only if key is an EXACT match.
Art of the Fugue: Bach ###### Dump of hash using "each" function!
Sonata for Two Pianos and Percussion: Bartok
Fur Elise: Beethoven
Art of the Fugue: Bach ###### Fur Elise/Beethoven deleted in this listing!
Sonata for Two Pianos and Percussion: Bartok
################ Summary of Relevant List/Hash Concepts ##################
1) Shift removes the leftmost (index 0) element of a list and returns the
shifted-off value.
2) Unshift places one or more elements onto the leftmost part of a list.
3) Pop removes the rightmost (highest index) element of a list and returns
the popped-off value.
4) Push places one or more elements onto the rightmost part of a list.
5) Use push or unshift to put new elements in a list, not:
@list = (@list, $stuff, $junk);
or
$list[$i] = $whatever; #### Avoid indices unless necessary!!
Unless you have a specific need for a list index variable (like $i), use
push or unshift. This speeds the programs performance and also means you
have one less variable to keep track of.
6) When using a function which returns a list of known size, collect
the elements like:
($name, $salary, $department) = function_returning_list();
and NOT
@person_data = function_returning_list()
It's much easier to tell what $name means than @person_data[0].
Remember this when using functions like split that return lists!!