Perl Assignment Level 0-2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Module 2: PERL

Assignment Level 0: No. 2


1. Write a while loop what will print squares of numbers 1, 2, 3, ..., 10.

2. What does the following program do?


--------------------------------------------------------
#!/usr/bin/perl
foreach $i ( (1..100) ){
$j = sqrt($i);
print "$j\n";
}
--------------------------------------------------------

3. Write a program to count the number of words in a text file "f1.txt".


[Hint: use perl functions "split" and "length".

4. Make a list of 5 mathematical functions (like sqrt()) available in perl.


[Hint: look-up perlfunc man-page].

5. Make a list of 5 unusual (i.e. which you did not find in C) perl operators. [Hint:
look-up any of the cheat-sheets provided or manpage perlop.]

6. A text file f2.txt contains name of state and its approx. population, (in crore of
people) one state on each line, like:
-------------------------------
Guajarat 4.5
Bihar 3.0
... ...
-------------------------------
Write a program to create a hash %states in memory, which uses state name as
the key and population as the value.

7. Extend the above program to print a table of state population, in alphabetical


order of states.

8. Extend the above program to print a table of state population, in ascending


order of population. [Hint: use sort {$a <=> $b} @array]
Module 2: PERL

9. Try the following little program and note your conclusions:


-------------------------------
#!/usr/bin/perl
@first = 1..5;
print "@first\n";
($f, $s, $t, $fr, $ff) = 1..5;
print "$f, $s, $t, $fr, $ff\n";
-------------------------------

10. Re-write your program in exercise 6, using idea you picked up from exercise 9.

You might also like