Declaring variables :-
$name="george";$phone="432232";
print "Name is :$name and Phone is :$phone \n";
Declaring arrays :-
#Single dimentional array example :
@items=(fan,frodge,pot,pen);
foreach $itm (@items)
{
print "$itm \n";
};
*Note :
Here \'@\' declares a single dimentional array.
And "foreach" is a for loop in PERL.
#Double dimentional array (Associative arrays)example :
Normally Associative arrays have "Keys" and "Values" ;
viz, Keys represent the first column value and Values
point to the second column value in an double
dimensional array.
eg:
%persons=("John.s",45,"Manoj.k",67,"Ron Jas",33);
print %persons;
# displays three lines; each line with name and
# corresponding age.
foreach $names{keys %persons}
{ print "Name is : $names \n"; }
# Displays only Names (ie KEYS).
foreach $ages{values %persons}
{ print "Age is : $ages \n"; }
# Displays only Ages (ie VALUES).
Finding a specific value in an array :
eg :-
@items=("Ball","Gloves","Frisby","Suit");
foreach $prods (@items)
{
if ($prods=~/Frisby/) {
print "A Flying game is $prods \n";
}
}