Sei sulla pagina 1di 9

ARRAYS, LISTS AND HASHES

By

SANA MATEEN

ARRAYS

It is collections of scalar data items which have an assigned storage space in memory, and can
therefore be accessed using a variable name.

The difference between arrays and hashes is that the constituent elements of an array are
identified by a numerical index, which starts at zero for the first element.

array always starts with @, eg: @days_of_week.

An array stores a collection, and list is a collection, so it is natural to assign a list to an array.
eg.
@rainfall=(1.2, 0.4, 0.3, 0.1, 0, 0 , 0);

This creates an array of seven elements. These can be accessed like


$rainfall[0], $rainfall[1], .... $rainfall[6].
A list can also occur as elements of other list.
@foo=(1,2,3, string);
@foobar= (4, 5, @foo, 6);
This gives foobar the value (4,5,1,2,3, string,6).

MANIPULATING ARRAYS

Elements of an array are selected using C like square bracket syntax, eg: $bar=$foo[2].

The $ and [ ] make it clear that this instance foo is an element of the array foo, not the
scalar variable foo.

A group of contiguous elements is called a slice, and is accessed using simple syntax.

@foo[1..3]

Is the same as the list


($foo[1],$foo[2],$foo[3])

The slice can be used as the destination of the assignment eg:@foo[1..3]= (hop, skip,
jump);

Array variables and lists can be used interchangeably in almost any sensible situation:
$front=(bob, carol, ted, alice)[0];
@rest=(bob, carol, ted, alice) [1..3];
or even
@rest=qw/bob carol ted alice/[1..3];
Elements of an array can be selected by using another array selector.

@foo =(7, fred, 9);

@bar=(2,1,0);

then
@foo=@foo[@bar];

LISTS

List is a collection of variables , constants or expressions which is to be treated


as a whole . It is written as comma separated sequence of values.
Eg: red, green, blue

A list often appears in a script enclosed in round brackets.


(red, green, blue)

Short hand used in lists: (1..8) and (A.. H, O.. Z).

To save the tedious typing, qw(the quick brown fox)

Is short hand for : (the, quick, brown, fox).

qw- quote words operator, is an obvious extension of the q and qq operator.

qw/the quick brown fox/ (or) qw|the quick brown fox|

The list containing variables can appear as the target of an assignment and/or as the
value to be assigned.
($a , $b , $c)= (1,2,3);

MANIPULATING LISTS

Perl provides several built-in functions for list manipulation. Three useful ones are a)shift
LIST b)unshift LIST c)push LIST

a) returns the first item of the list and moves remaining items down reducing the size of the
list by 1.

b) the opposite of shift: puts the items in LIST at the beginning of ARRAY, moving the
original contents up by the required amount.

c) push LIST: It is similar to unshift but adds the values in LIST to the end of ARRAY

ITERATING OVER LISTS


Perl provides a number of mechanisms to achieve this.

I.

foreach

II.

map

III.

grep

foreach loop: It performs a simple iteration over all the elements of a list.
foreach $item (list){
}
This blocks takes each value from the list and repeats execution.
foreach (@array){
.... #process $_

map: perl provides an inbuilt function map to create plural forms of words.

@p1=map $_. s , @s;

general form of map is: map expression, list;

and map BLOCK list;

we can also use foreach loop to achieve the same.


@s=qw/cat, dog, rabbit, hamster, rat/;
@p1=();
foreach (@s){
push @p1, $_. s
}

grep : In unix grep is used to print all lines of the file which contains an instance of
pattern.
grep pattern file

The perl grep function takes a pattern and a list and returns new list containing all the
elements of the original list that match the pattern.
Eg: @things = (car, bus, cardigan, jumper, carrot);
grep /car/ @things
returns the list
(car,cardigan,carrot)

HASHES

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To
refer to a single element of a hash, you will use the hash variable name preceded by a "$"
sign and followed by the "key" associated with the value in curly brackets.

Here is a simple example of using the hash variables

#!/usr/bin/perl

%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);

print "\$data{'John Paul'} = $data{'John Paul'}\n";

Key

Value(age)

print "\$data{'Lisa'} = $data{'Lisa'}\n";

print "\$data{'Kumar'} = $data{'Kumar'}\n";

John Paul

45

This will produce the following result

Lisa

30

$data{'John Paul'} = 45

Kumar

40

$data{'Lisa'} = 30

$data{'Kumar'} = 40

CREATING HASHES
we can assign a list to an array, so it is not surprising that we can assign a list of key-value pairs to
a hash.
for example:
%foo= (key1, value1, key2, value2,.....);
alternative syntax is provided using the => operator to associate key-value pairs

%foo =(banana => yellow , apple=>green , ...)

MANIPULATING HASHES

Perl provides a number of built-in functions to facilitate manipulation of hashes. If we


have a hash called magic.
keys %magic
Returns a list of the keys of the elements in the hash.
values %magic
These functions provide way to iterate over the elements of hash using foreach:
foreach $key(keys %magic) {
do something with $magic($key)
}
Explicit loop variable is omitted, in which case the anonymous variable $_ will be assumed.
foreach(keys %magic)
{
process $magic($_);

}
An alternative is to use each operator which delivers successive key-value pairs from a hash.
while(($key,$value)=each %magic){
...
}

Other useful operators for manipulating hashes are delete and exists.
delete $magic($key)
Removes the elements whose key matches $key from the hash %magic, and
exists $magic($key)
Returns true if the hash %magic contains an element whose key matches
$key.common idiomis
exists($h{key})&&do(statements)

To avoid using an if statement.

Potrebbero piacerti anche