You are on page 1of 11

Splice():

 Replaces a set of array elements.


Syntax:
splice(array, offset, length, list);
Replaces elements of ‘array’, starting from the position
‘offset’ up to ‘length’ no. of elements, with the elements
of the ‘list’.
If ‘length’ is omitted, all the elements from the ‘offset’
are replaced.

map():
 Allows to change all the elements of the array.
 Transforms one list of values into another list.
Syntax:
map(expression, array);

Ex: Program to search for an array element


#!/usr/bin/perl

@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

Limitations of array variables:


 Accessing an array element is done by specifying a
subscript, which is an integer.
Ex: @arr=(10..20);
print $arr[3];
 But it is difficult to remember which element of an
array stores what.
 For example, we are storing the quantities of the
fruits like banana, cherries, oranges, apples, kiwi in an
array:
@fruits=(100,20,56,90,120);
 It’s very difficult to remember which value is
representing which fruit, because they are accessed
using integer indices.
 To access the quantity of apples, we need to
remember the order and have to write the statement
like $fruits[3].

 It’s difficult to remember the order, in which the


fruits are stored. So, we can use hashes (or) associative
arrays.
 In hashes, we can store not only the values, but
also the indices. We can even use string indices, which
are easy to remember compared to the integer indices.

 Perl defines another kind of array called


“associative arrays”, which enable us to access array
variables using any scalar value we like.
 The name of the hash starts with a % sign, followed
by a letter and the subsequent characters can be letters,
digits (or) underscores.

Creating an associative array

 %fruits=(“apples”,80, “oranges”, 100, “cherries”,


120, “kiwi”, 90, “banana”, 160);
 (0r)
 %fruits=(“apples” => 80, “oranges”=> 100,
“cherries”=> 120, “kiwi” => 90, “banana”=> 160);
 Here, elements are stored as a (key,value) pair i.e.
not only the value, the index(key) used to access the
value is also stored in the hash.
 Here, element with the index “apples” is 80, index
“oranges” is 100 and so on. So, it is easy to remember
what is what.
 The elements of an associative array are not stored
in any particular order.
 We can use either , or => to separate array
subscripts and their values.
 It will be easy to recognize which subscript is
associated with which value.
 To access elements of a hash, we will use $ and { }.

Accessing the hash elements:

Ex:%fruits=(“apples” => 80, “oranges”=> 100, “cherries”=>


120, “kiwi” => 90, “banana”=> 160);
To access the quantity of cherries,
print $fruits{“cherries”};
 While accessing an independent hash value, we
prefix the name with ‘$’ only, not with %.
 Subscripts for hashes are entered in { }, not in [ ].
This distinguishes between array variables and hashes.

Adding a new element to a hash.


 %fruits=(“apples” => 80, “oranges”=> 100,
“cherries”=> 120, “kiwi” => 90, “banana”=> 160);
To add a new element to it, we have to say;
$fruits{“butterfruit”}=89;
 Perl checks if any index “butterfruit” exists in the
hash (or) not. If yes, it updates the value. If not, it will be
created new.
Ex:
$fruits{“apples”}=100;
As “apples” is existing already, its value is updated from
8o to 100.

Deleting an element from the hash


 To delete an existing element, we can use the built-
in function delete().
Ex:
delete $fruits{“oranges”};
 It’s the only way to delete elements of a hash.
 Push(), pop(), shift(), unshift() can’t be applied on
hashes, as they work on the last or the first positions
specifically. But hash elements are not stored in an
order.

Accessing hash values using keys()


 Keys() retrieves the list of subscripts used in an
associative array.
Ex:
%sal=("smith"=>19000,"blake"=>80000,"clark"=>78000,
"alice"=>19000,"smith",40000);

foreach $i(keys %sal)


{
print "key is:$i\n";
print "value is:$sal{$i}\n";
}

Accessing hash elements using values()


 Values() gives the list of values present in a hash.
%sal=("smith"=>19000,"blake"=>80000,"clark"=>78000,"alice
"=>19000,"smith",40000);
foreach $i(values %sal)
{
print "$i\n";
}

Copying associative array from array variable

@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.

Copying one associative array into another


 %fruits=(“apples”,100,”oranges”,200,”cherries”,90)
;
%fruits_copy=%fruits

Copying associative array to a normal array


%fruits=(“apples”,100,”oranges”,200,”cherries”,90);
@arr=%fruits;
Now, an array is created with 6 elements, and the (key,value)
pair relation ship between them is no longer maintained.

Finding the length of the hash


%sal=("smith"=>12000,"jones"=>9000,"david"=>7000,"alice"
=>56000,"john"=>9000);
@x=%sal;
$len=@x;
print "length of the hash=",$len/2,"\n";

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.

Getting the size of the hash (length of the hash):


 We can get the size of the hash by first assigning to
an array and there by finding the length of that array.
 divide the length of the array with 2 to get exact
length of the hash.
Ex:
%sal=("smith"=>19000,"blake"=>80000,"clark"=>78000,
"alice"=>19000,"smith",40000);
@arr=%sal;
$cou=@arr/2;
print "len of hash=$cou\n";

You might also like