As we have seen in all the languages we have studied thus far, variables are an important
and sometimes confusing concept. In C++ and JAVA there are numerous types of variables,
many of which are extremely similar. the only real difference is how the variables are
stored, deep down in the implementation of the language. Most casual users of the big two
languages know that int is for whole numbers, and float is for
real numbers, but to make things confusing there is also a double which is the
same thing as a float really. What about the addition of items such as long
which can be used alone, or with other variable names? This is all very confusing, and in
the long run, it doesn't really matter, unless you are really particular about
such things.
Perl tries to insulate the user from such restrictions. Why should the programmer be burdened with information such as in what internal format a number will be stored with? In Perl, all the guess work has been taken out of variables. There are 3 basic kinds of variables in Perl:
#!/usr/local/bin/perl -w
$number = 5;
$string = "I am a string";
print "number: ", $number, "\n\n";
print "string: ", $string, "\n\n";
The output of this little script looks like so:
number: 5
string: I am a string
See how easy it is? No need to go declaring libraries, strings, integers, and the like.
We will see more on how to manipulate these values later. It is interesting to note
that when you want to print many things, you can separate them by a comma (this is similar
to JAVA, except in JAVA you would use a +).
#!/usr/local/bin/perl -w
@number_array = (1, 2, 3, 4);
@string_array = ("String 1", "String 2", "String 3");
@mixed_array = ("Element 1", 2, "6");
print "A Single Element: (Good Way) ", $number_array[0], "\n\n";
print "A Single Element: (No So Good Way) ", @number_array[0], "\n\n";
foreach $number (@number_array) {
print "number_array: ", $number, "\n";
}
print "\n";
foreach $string (@string_array) {
print "string_array: ", $string, "\n";
}
print "\n";
foreach $mix (@mixed_array) {
print "mixed_array: ", $mix, "\n";
}
print "\n\nGoing Beyond The End Of An Array...\n\n";
for ($number = 1; $number <= 10; $number++) {
print "mixed_array: ", $mixed_array[$number], "\n";
}
print "\n\n";
The output of this little script looks like so:
A Single Element: (Good Way) 1 A Single Element: (No So Good Way) 1 number_array: 1 number_array: 2 number_array: 3 number_array: 4 string_array: String 1 string_array: String 2 string_array: String 3 mixed_array: Element 1 mixed_array: 2 mixed_array: 6 Going Beyond The End Of An Array... mixed_array: 2 mixed_array: 6 Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array: Use of uninitialized value in print at ./Variables.pl line 41. mixed_array:The loops will be discussed later on, but as you can see it is very easy to store and use the array concept.
#!/usr/local/bin/perl -w
%students = ("Bob", 80, "Sue", 99, "Frank", 67, "Ted", 65, "Jane", 78);
print "Frank\t", $students{"Frank"}, "\n\n";
@keys = keys(%students);
foreach $key (@keys) {
print $key, "\t", $students{$key}, "\n";
}
print "\n";
# If you sort the keys, you can get the hash out in order.
@keys = sort(@keys);
foreach $key (@keys) {
print $key, "\t", $students{$key}, "\n";
}
@array = ("One", "1", "Two", "2", "Three", "3");
%hash = ();
print "\n";
for($var = 0; $var <= 4; $var += 2) {
$hash{$array[$var]} = $array[$var+1];
}
@keys = keys(%hash);
foreach $key (@keys) {
print $key, "\t", $hash{$key}, "\n";
}
The output of this little script looks like so:
Frank 67 Sue 99 Bob 80 Frank 67 Jane 78 Ted 65 Bob 80 Frank 67 Jane 78 Sue 99 Ted 65 Three 3 Two 2 One 1It is important to note that this output is entirely random. The elements in a hash are not kept in any sorted order (this makes the whole thing run a lot faster). If you wish to get the elements in a sorted order, it is necessary to use some sort of sorting function, as in the second example above.
%hash = ();. This is basically a way to clear the hash out, in case
it already had some stuff in it. This will eliminate errors in the long run.[] notation, using a loop
or accessing via the $hash_name{key} are the only methods. There is
also a function to extract the values (similar to extracting the keys), but more on
that later.
All variable names in Perl can be up to 255 characters long, and can contain any letter, number, or underscore; no other punctuation is allowed. You must always remember to place the appropriate symbol at the start of the name ($ for scalar, @ for array, or % for hash). Declaring variables in Perl is not a requirement, as you can see from my examples. As I start to use the variable (initialize it), that is good enough for the interpreter. As we learn more about Perl, you will see that there are ways to declare variables to go along with different scopes.
Besides the three major ways of using variables, you also have the option to dynamically create and define your own variables. This is similar to JAVA and C++, and is one reason that Perl can now be classified as an Object Oriented Language.
The last variable concept we will discuss is the notion of a multi-dimensional array. There
is no specific type for this, but you can easily create one using arrays, hashes, and
scalars. Lets see an example:
#!/usr/local/bin/perl -w
@Robila = ("CMPT 183 ", "CMPT 109 ");
%teachers_classes = (
Deremer => ["CMPT 588 ", "CMPT 280 "],
Koeller => ["CMPT 586 ", "CMPT 109 "],
Johnson => ["CMPT 372 "],
Bredlau => ["CMPT 583 ", "CMPT 112 ", "CMPT 109 "],
Boyno => ["CMPT 184 "],
);
# adding an array to the structure
$teachers_classes{"Robila"} = [@Robila];
foreach $teacher (keys %teachers_classes) {
print $teacher, "\t", @{$teachers_classes{$teacher}}, "\n"
}
print "\n";
# add another (but not as a stored array) to the structure
$teachers_classes{Benham} = ["CMPT 576 ", "CMPT 109 "];
foreach $teacher (keys %teachers_classes) {
print $teacher, "\t", @{$teachers_classes{$teacher}}, "\n"
}
The output of this little script looks like so:
Bredlau CMPT 583 CMPT 112 CMPT 109
Deremer CMPT 588 CMPT 280
Johnson CMPT 372
Robila CMPT 183 CMPT 109
Boyno CMPT 184
Koeller CMPT 586 CMPT 109
Bredlau CMPT 583 CMPT 112 CMPT 109
Deremer CMPT 588 CMPT 280
Johnson CMPT 372
Benham CMPT 576 CMPT 109
Robila CMPT 183 CMPT 109
Boyno CMPT 184
Koeller CMPT 586 CMPT 109
This is what is known as a 'Hash Of Arrays', mainly because we are storing
array elements inside of a hash. It is possible to store the other 3 possibilities as well:
'Hash Of Hashes', 'Array Of Hashses', and 'Array Of Arrays' are all
similar in nature.
With all of these building blocks, it is possible to create very accurate and complete data structures.