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 

Comments

Anonymous at 22 Sep, 2007 04:58
Thanks for posting this info. It was very helpful.
Reply to this.
Anonymous at 28 May, 2008 10:38
FortTran starts array indexes at 1 by default; however, it allows you to start the array index at any value you wish also. most languages not based on C start array indexes at 1 actually... basic, pascal, ForTran, ..... I am not sure about business languages (such as Cobalt) though.

Try not to refer to the popular operating system, web, and server languages as the Major languages. If you want to call out program names say what they are used for, do not clump them as the major programs.

C/C++ --operating systems and GUI interfaces

Perl --Scripting language for server application, and some neat application, but it runs at a snails pace.

Jave --web applications

ForTran --Scientific, engineering, and research application requiring large amounts of computation.

Cobalt --business language (bank computers and much accounting software)

LISP --parsing language

Prolog --artificial intelligence research
Reply to this.
Anonymous at 28 May, 2008 10:39
not to be a total, critic.... I am enjoying the website, and learning some things about Perl.

Thanks
Reply to this.
Anonymous at 25 Sep, 2008 09:52
its my first language but from I have seen it depends on how you build it if it will be slow or not.
Reply to this.
Hill at 05 Dec, 2008 12:30
this is a great site teaching basic learners like me how to program step by step with clear explanation.
Thanks.
Reply to this.
Daisy at 11 Jan, 2010 01:30
I am begining on perl... this site is really helpful
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed