Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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);
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]
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.
@bar=(2,1,0);
then
@foo=@foo[@bar];
LISTS
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
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.
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.
#!/usr/bin/perl
Key
Value(age)
John Paul
45
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
MANIPULATING HASHES
}
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)