Previous
Hello World 
Next
Operators 
Beginner's Tutorial for CGI using Perl Language
Variables, Arrays and Hashes

Variables, Arrays and Hashes

Technically speaking, Perl has three main variable types: scalars, arrays, and hashes. I will try to keep this as simple as possible.

Scalars - These are the variables that represents a single value. These variables always has a "$" (dollar sign) before it:

my $name = "Binny";
$phrase = "Ain't this cool";
$day = 12;

The 'my' keyword can be used if you want a local variable. Local variable is the variable that exist only in the block that it is defined in. It is safer to use 'my' while initializing variables. An easy method to make sure that you have done this is to use the 'strict' module. You can do this by including 'use strict;' at the top of the file. More about that in the modules section.

Now we move on to arrays - If you have any experience in programming you might have heard this word before. If not, you have heard it now. Array variables are variables that hold multiple values. All values can be accessed individually with its index. Now please remember this - arrays usually have a '@' symbol at the beginning when it is accessed as whole. But when declaring slots individually, you should use a '$' sign. Feeling lost? An example will set things straight.


0. #!/usr/local/bin/perl
1. #Make array
2. my @about_me = ("Binny", "http://www.geocities.com/binnyva", 13,
	"Student, Programmer, Self-Employed");
3. #Print the details
4. print "Name : $about_me[0]\n";
5. print "Website : $about_me[1]\n";
6. print "Lucky Number : $about_me[2]\n";
7. $about_me[2]++; #Modify number.
8. print "New Number : $about_me[2]\n";
9. print "Current Status : $about_me[3]\n";
10. #Adding new items
11. $about_me[4] = "C++, Perl, Tcl/Tk, HTML, English etc.";
12. $about_me[5] = "You won't get my email address you spammers.";
13. print "Languages Known : $about_me[4]\n";
14. print "E-Mail : $about_me[5]\n";
15.
16. print "The whole array : @about_me\n";

Explanation of the program...
Line one is a comment, so we will be leaving that. The next line
my @about_me = ("Binny", "http://www.geocities.com/binnyva", 13, "Student, Programmer, Self-Employed");
creates the local array called '@about_me'. The stuff inside the brackets - "Binny", "http://www.geocities.com/binnyva" etc are values that will be present in the array. This line will create a array named 'about_me' and give it 4 values.

The next line is the print "Name : $about_me[0]\n"; line. This will print the string "Name : Binny". Notice the term '$about_me[0]' in this line? This will tell the perl interpreter to access the first value in the 'about_me' array. But why didn't I type $about_me[1] to get the first value? This is another important thing for you to remember. The array starts at 0 - NOT at 1. So 0 is the first element, 1 is the second element and so on. This is true for all major languages like C++, Java, Perl etc. I don't know even one language where this rule don't hold. Maybe in one of the older languages. If any of my readers know of such a language, please send me a note.

The next two lines, line number 5 and 6, prints other stings. Then comes line number 7. This modifies an existing value in the array. This takes the third value(13) and increments it(adds 1 to it). So the result is 14. This is stored in the third slot of the array. Next line prints this number.

Moving on to the 11th line, we add a new item to the existing array. This is the fifth item so the index is 4(remember array starts at 0). We give it a new value and then print it. The final line, 14th, will print the full array - did you notice that I used the '@' symbol before the array name? It should be used when one is accessing the array as a whole.

OK. Your knowledge about perl has multiplied a thousand times by now. That is because when you started reading this tutorial, your knowledge about perl was 0. Now let us move on to hashes - also known as associative arrays.

A hash represents a set of key/value pairs. This is great for storing related information. It can be used as the array is used - but no index number is necessary - you can use a sting in its place. Below given is an example of hashes.


#!/usr/local/bin/perl
use strict;

print "Medical Dictionary for Rednecks and Blonds\n";
my %meanings = (
	Artery   => "The study of painting",
	Bacteria => "The back door of the cafeteria",
	Barium   => "What the doctors do when patients die",
	Bowel    => "A letter like A, E, I, O, or U",
	Seizure  => "A Roman emperor",
	Tablet   => "A small table",
	Tumor    => "More than one",
	Urine    => "Opposite of you're out"
    );
print "Meaning of Artery is $meanings{'Artery'}
Tumor means $meanings{'Tumor'} while Seizure is $meanings{'Seizure'}";

More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. For more information on how to do this, refer the perl manual.

Previous
Hello World 
Next
Operators 
Subscribe to Feed