Upload
siddharth-verma
View
223
Download
0
Embed Size (px)
Citation preview
8/11/2019 Introduction to Perl Day2
1/23
Perl Language
Day 2
8/11/2019 Introduction to Perl Day2
2/23
Collections Of Variables: Arrays
8/11/2019 Introduction to Perl Day2
3/23
3
Arrays
Array variable is denoted by the @ symbol @array = ( Larry, Curly, Moe );
To access the whole array, use the wholearray print @array; # prints : Larry Curly Moe
Notice that you do not need to loop throughthe whole array to print itPerl does this foryou
8/11/2019 Introduction to Perl Day2
4/23
4
Arrays cont
Array Indexes start at 0 !!!!!
We can assign an array directly to another array.
E.g. @array_1 = @array_2;
To access one element of the array : use $ Why? Because every element in the array is scalar
print $array[0]\n; # prints : Larry
Question: What happens if we access $array[3] ?
Answer1 : Value is set to 0 in Perl
Answer2: Anything in C!!!!!
8/11/2019 Introduction to Perl Day2
5/23
5
Arrays cont ...
To find the index of the last element in thearrayprint $#array; # prints 2 in the previous
# example
Note another way to find the number ofelements in the array:$array_size = @array;
$array_size now has 3 in the above examplebecause there are 3 elements in the array
8/11/2019 Introduction to Perl Day2
6/23
6
Sorting Arrays
Perl has a built in sort function
Two ways to sort:
Default : sorts in a standard string comparisons order
sort LIST Usersub: create your own subroutine that returns an
integer less than, equal to or greater than 0
Sort USERSUB LIST
The and cmp operators make creating sortingsubroutines very easy
8/11/2019 Introduction to Perl Day2
7/23
7
Numerical Sorting Example
#!/usr/local/bin/perl -w
@unsortedArray = (3, 10, 76, 23, 1, 54);
@sortedArray = sort numeric @unsortedArray;
print @unsortedArray\n; # prints 3 10 76 23 1 54print @sortedArray\n; # prints 1 3 10 23 54 76
sub numeric
{
return $a $b;
}
# Numbers: $a $b : -1 if $a$b
# Strings: $a cmp $b : -1 if $a$b
8/11/2019 Introduction to Perl Day2
8/23
8
#!/usr/local/bin/perl -w
@unsortedArray = (Larry, Curly, moe);
@sortedArray = sort { lc($a) cmp lc($b)} @unsortedArray;
print @unsortedArray\n; # prints Larry Curly moeprint @sortedArray\n; # prints Curly Larry moe
String Sorting Example
8/11/2019 Introduction to Perl Day2
9/23
9
Foreach
Foreach allows you to iterate over an array
Example:foreach $element (@array)
{
print $element\n;
}
This is similar to :for ($i = 0; $i
8/11/2019 Introduction to Perl Day2
10/23
10
Sorting with Foreach
The sort function sorts the array and returns the list insorted order.
Example :
@array( Larry, Curly, Moe);foreach $element (sort @array)
{
print $element ;
}
Prints the elements in sorted order:
Curly Larry Moe
8/11/2019 Introduction to Perl Day2
11/23
11
Exercise: Sorting According to
Multiple Criterion
@index=(0,1,2,3,4,5);@name=(V,W,X,Y,Z);@age=(10,20, 15, 20, 10);@income=(100,670, 280,800,400);
foreach $i ( sort my_numeric @index){
print $name[$i] $age[$i] $income[$i];}
sub my_numeric
{
if ($age[$a] == $age[$b])
{return $income[$a]$income[$b]; }
else
{return $age[$a]$age[$b]; }
}
O/P :: V -- 10100W -- 20670
X -- 15 -- 280
Y -- 20800
Z -- 10 -- 400
8/11/2019 Introduction to Perl Day2
12/23
Sample Programs
1. Write a program to print the odd numbers from 1 to 500?
O/P :: 1,3,5,7,9,11, .
my $i = 0;
for ($i = 1; $i
8/11/2019 Introduction to Perl Day2
13/23
Manipulating Arrays
8/11/2019 Introduction to Perl Day2
14/23
14
Strings to Arrays : split
Split a string into words and put into an array
@array = split( /;/, Larry;Curly;Moe );
@array= (Larry, Curly, Moe);
# creates the same array as we sawpreviously
Split into characters
@stooge = split( //, curly );
# array @stooge has 5 elements: c, u, r, l, y
8/11/2019 Introduction to Perl Day2
15/23
15
Split cont..
Split on any character
@array = split( /:/, 10:20:30:40);
# array has 4 elements : 10, 20, 30, 40
Split on Multiple White Space
@array = split(/\s+/, this is a test;
# array has 4 elements : this, is, a, test
More on \s+ later
8/11/2019 Introduction to Perl Day2
16/23
16
Arrays to Strings
Array to space separated string
@array = (Larry, Curly, Moe);
$string =join( ;, @array);
# string = Larry;Curly;Moe
Array of characters to string
@stooge = (c, u, r, l, y);
$string =join( , @stooge );
# string = curly
8/11/2019 Introduction to Perl Day2
17/23
17
Joining Arrays cont
Join with any character you want
@array = ( 10, 20, 30, 40 );
$string =join( :, @array);
# string = 10:20:30:40
Join with multiple characters
@array = 10, 20, 30, 40);
$string =join(->, @array);
# string = 10->20->30->40
8/11/2019 Introduction to Perl Day2
18/23
18
Arrays as Stacks and Lists
To append to the end of an array :
@array = ( Larry, Curly, Moe );
push(@array, Shemp );
print $array[3]; # prints Shemp
To remove the last element of the array (LIFO)
$elment = pop@array;
print $element; # prints Shemp @array now has the original elements
(Larry, Curly, Moe)
8/11/2019 Introduction to Perl Day2
19/23
19
Arrays as Stacks and Lists
To prepend to the beginning of an array@array = ( Larry, Curly, Moe );
unshift @array, Shemp;
print $array[3]; # prints Moeprint $array[0]; # prints Shemp
To remove the first element of the array$element = shift@array;print $element; # prints Shemp
The array now contains only :
Larry, Curly, Moe
8/11/2019 Introduction to Perl Day2
20/23
20
Exercise: Spliting
Instructions Remove
shift: beginning, pop: end
Add Unshift: beginning, push: end
Use split, shift and push to turn the following string:
The enquiry 1 was administered to five couplesThe enquiry 2 was administered to six couples
The enquiry 3 was administered to eigh couples
Intofive couples were administered the enquiry 1.
8/11/2019 Introduction to Perl Day2
21/23
21
Exercise: Spliting
Use split, shift and push to turn the following string:
$s[0]= The enquiry 1 was administered to five couples;$s[1]= The enquiry 2 was administered to six couples;$s[2]= The enquiry 3 was administered to eigh couples;
foreach $s(@s)
{
@s2=split (/was administered to/, $s);
$new_s=$s2[1] were admimistered $s2[0];
print $new_s\n;
}
8/11/2019 Introduction to Perl Day2
22/23
Multidimentional Arrays
8/11/2019 Introduction to Perl Day2
23/23
23
Multi Dimensional Arrays
Better use Hash tables (cf later)
If you need to:
@tab=([Monday,Tuesday],
[Morning,Afternoon,Evening]);$a=$tab[0][0] # $a == Monday
$tab2=(midnight, Twelve);
$tab[2]=\@tab2 # integrate tab2 as the last rowof tab