.:: Welcome To My Personal Blog ::.

Saturday, March 5, 2011

Beginner's Guide To Travel With Perl - Part 3

Variables

It’s time to say something about variables now. Perl has three types of variables: scalars, arrays and hashes. Let’s think of them as “things”, “lists” and “dictionaries”. All variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores in Perl.




Scalar:

Scalars are single element. This can be a number or a string. The name of a scalar starts with a dollar sign ($) with its name following the sign. $a, $name, $num etc might be some example for representing variable scalar in Perl. To assign a value to a scalar is so simple like other languages. It is done by using the assignment operator (=) simply.
Let’s see some examples :

Example1:
$num = 5;
print “The number is $num”;

The output will be “The number is 5”

Example2:
$name = “abc”;
print “The name is $name”;

The output will be “The name is abc”

Arrays:
Arrays are lists of Scalars. The names of arrays start with @ following the array name. Arrays are defined by listing their contents in parentheses, separated by commas. Examples are given below:

Example:
@numbers = (1, 2, 3, 4, 5);
@names = ("aa", "bb", "cc", "dd", "ee");
print "second number in numbers is $numbers[1]\n";
print "fourth name in names is $names[3]";

The output will be the following:
“second number in numbers is 2
fourth name in names is dd”

Hashes:
Hashes are called “dictionaries” in some programming languages. In more correct language Hashes have a key and a value. Each key in a hash has one and only one corresponding value. The name of a hash begins with a percentage sign (%). Hashes are defined by comma-separated pairs of key and value. Examples are given below:
%char_seq = ( "a" => 1, "b" => 2, "c" => 3 );

It is possible to fetch any value from a hash by referring to $hashname{key}, or modify it in place just like any other scalar.
print $char_seq{"b"}; #Output will be 2
$char_seq{"a"} = 26; #value modified
If we want to see what keys are in a hash, we can use the keys function with the name of the hash. This returns a list containing all of the keys in the hash. The list isn't always in the same order as the original one. Keys %char_seq might return them in any order whatsoever.

@char_list = keys %char_seq; # @char_list is now ('c', 'a', 'b')

The three types of variables have three separate namespaces. That means that $num and @num are two different variables, and $num[0] (the first element of @num) is not the same as $num{0} (the value in num that has the key 0).


Help Links:

http://www.perl.com/pub/2000/10/begperl1.html
http://perldoc.perl.org/perlintro.html

http://www.perl.org/learn.html
http://learn.perl.org/books.html
http://learn.perl.org/tutorials/
http://www.perl.com/pub/2008/04/23/a-beginners-introduction-to-perl-510.html
http://docstore.mik.ua/orelly/perl2/prog/ch01_05.htm
http://www.tizag.com/perlT/perluserinput.php
http://www.tizag.com/perlT/perlchomp.php

http://perldoc.perl.org/functions/use.html
http://www.well.ox.ac.uk/~johnb/comp/perl/intro.html

http://www.sthomas.net/roberts-perl-tutorial.htm/ch22/use_strict_
http://docstore.mik.ua/orelly/perl/prog3/ch31_19.htm
http://debugger.perl.org/580/perldebtut.html
http://perldoc.perl.org/strict.html


. . . . . To be continued . . . . .

1 comment:

Popular Posts