• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

Variables in Perl

User avatar
Usman55
VIP - Site Partner
Posts: 2821

Variables in Perl

Mon Mar 31, 2014 12:32 pm

Variables are locations of reserved memory used to store data. Based on the type of data, different variables can be created. Variables can be used to store strings, integers etc.

Perl supports 3 types of variables:
Scalar (preceded by a $ sign)
Arrays (preceded by a @ sign)
Hashes (preceded by a % sign)

Perl stores these three types of variables in separate namespaces. This means that the same variable name can be used for a scalar, array and hash variable without confusing the script.

Declaring a variable is very easy. You put a symbol ($, @ or %) followed by a name for the variable, an equal sign and the data to be set for the variable.

Scalar Variables:
Scalars are the simplest type of variables. They can hold integers and strings. The $ sign is used to define/declare a scalar variable. Numbers (including fractions and decimals) can be typed straight away but strings need to be written within double quotations. Some examples are:

Code: Select all

$city = "Doha";
$name = "Usman";
$age = 16;
$cent = 0.1;
Variables can be used to define other variables which have been declared before. Example:

Code: Select all

$city = "Doha";
$text = "I live in $city";
When we will print $text, the result will be "I live in Doha".

Array Variables:
An array variable holds an array of values. Array can also be called as a list or collection. An array can be defined by using the @ sign. An array can hold an indefinite number of values whether strings or integers. The collection of values is placed within parentheses. Example:

Code: Select all

@cities = ("Doha", "Al-Khor", "Al-Wakra");
Each value is written in separate quotation marks and separated by commas. Each value in the array is assigned a numeric ID or index. This index starts from 0. So the first value in the array will have the ID 0. We can use IDs to call upon a value at a certain position in the array. For example:

Code: Select all

print "$cities[0]\n";
The above code will print the string "Doha". When calling a value from an array, the $ sign is used instead of @ as the value we're calling upon is a scalar.
To print all the values in an array, it can be do so by repeting the above code for each ID. Or it can be done by a single command, like this:

Code: Select all

print "@cities";
This code will return all the values as a single string. To get a list of the values as separate strings, we'll use the foreach method:

Code: Select all

foreach $i (@cities)
{
    print "$i\n";
}
Hash Variables:
These are special types of arrays called associative arrays. Each entry in hash variables is a pair of values. In arrays, we used numeric IDs but in a hash, we use keys. Hash variabled are preceded with the % sign instead of the @ sign. Example:

Code: Select all

%residence = ("Usman", "Al-Wakra", "Cody", "America", "John", "Netherlands");
As with arrays, if you have to refer to a value in a hash variable, the $ sign will be used. But instead of square brackets, curly brackets will be used. Referring to a value present in the hash variable returns the corresponding key. For example:

Code: Select all

$residence{"Cody"};
The resulting string that will be printed is "America".
To get all the values, we can use foreach method as follows:

Code: Select all

foreach $key (keys %residence)
{
    print "$key is from $residence{$key}\n";
}
Note: All codes in Perl are case sensitive. So when you declare a variable, that too will be case sensitive. This means $apple is not the same as $Apple. Also, don't forget to add a semi-colon at the end of statements.
Image

Post Reply

Return to “Tutorials”