Professional Documents
Culture Documents
map():
Allows to change all the elements of the array.
Transforms one list of values into another list.
Syntax:
map(expression, array);
@arr=(10,20,30,40,50);
print "enter the element to be searched:";
$key=<STDIN>;
chop($key);
$ind=0;
foreach $i(@arr)
{
if($key==$i)
{
print "$key is found in the array at the index $ind\n";
irint "@arr\n";
exit;
}
$ind++;
}
print "$key is not found in the array\n";
Associative Arrays/Hashes
@fruit=(“apples”,100,”oranges”,200,”cherries”,90);
%fruits=@fruit;
Now, an associative array is created with 3
elements.
When we are assigning an array variable to a list,
we have to make sure that the list contains even
number of elements. This is because, each pair of
elements corresponds to the subscript and the value of
an associative array element.
each() function
Perl provides a more efficient way to work with
associative array subscripts and their values, using the
built-in function “each()”.
Syntax:
($index,$value)=each(hash name)
Every time ‘each()’ is called, it returns a two-
element list.
The first element of the list is the subscript for an
element of the hash.
The second element is the value associated with
that particular subscript.