Extracting Information-Environment and Languages for Programming-Lab Task, Lecture notes of Programming Languages

Prof. Gaurav Bhatt assigned this lab task for Environment and Languages for Programming course at Institute of Company Secretaries of India - ICSI. Its main parts are: Extracting, Information, Modifications, Executable, Perl, Script, Command, Expressions, Unsorted, Array

Typology: Lecture notes

2011/2012

Uploaded on 07/16/2012

sambandan
sambandan 🇮🇳

4.7

(3)

35 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#! / u sr / b i n / pe r l
# Au thor: < fi ll i n yo ur n ame >
# Pe r l s cr ip t c o nt a in in g C O MP 2 84 Pe r l e xe rc i se s .
$l og 1 = " G en e r at i n g a n u ns o r te d a rr a y t oo k 1 . 25 9 s e c on d s \ n " ;
$l og 1 .= " S o rt in g t oo k 1 0 .4 86 s ec o nd s \ n ";
$l og 1 . = " G en e r at i n g a n u ns o r te d a rr a y t oo k 1 . 34 6 s ec o n ds \ n " ;
$l og 1 .= " S o rt in g t oo k 9 . 27 6 s ec o nd s \ n" ;
print $l og 1 ;
b. Save the code to a file named perlExercise2 in some appropriate directory.
c. Open a terminal, go to the directory in which the file has been stored.
d. Make sure that the file perlExercise2 is executable by using the command
chmod u+x perlExercise2
Remember that you only have to do so once. When you later save the file again
after making modifications, the file permissions will be preserved and the file remains
executable.
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Extracting Information-Environment and Languages for Programming-Lab Task and more Lecture notes Programming Languages in PDF only on Docsity!

#!/ usr / bin / perl

Author : < fill in your name >

Perl script containing COMP284 Perl exercises.

$log1 = " Generating an unsorted array took 1.259 seconds \ n "; $log1 .= " Sorting took 10.486 seconds \n "; $log1 .= " Generating an unsorted array took 1.346 seconds \ n "; $log1 .= " Sorting took 9.276 seconds \n "; print $log1 ;

b. Save the code to a file named perlExercise2 in some appropriate directory.

c. Open a terminal, go to the directory in which the file has been stored.

d. Make sure that the file perlExercise2 is executable by using the command chmod u+x perlExercise Remember that you only have to do so once. When you later save the file again after making modifications, the file permissions will be preserved and the file remains executable.

e. Now execute the Perl script using the command ./perlExercise and check that the output is: Generating an unsorted array took 1.259 seconds Sorting took 10.486 seconds Generating an unsorted array took 1.346 seconds Sorting took 9.276 seconds

  1. In the lectures we have looked at the use of regular expressions to extract information.

a. Add the following code at the end of your Perl script, save the file and then execute it. $_ = $log1 ; (/ Sorting took (\ d +.\ d +) seconds /) && do { $runtime += $1 ; $count ++; print "1: Match found : $ 1 -- $runtime -- $count \ n "; };

b. Check that the additional output is 1: Match found : 10.486 -- 10.486 -- 1

c. Refer to http://perldoc.perl.org/functions/do.html

to understand what the function of do is.

  1. In the last example, we found only one match. Let us try to find all matches.

a. Add the following code at the end of your Perl script, save the file and then execute it. $runtime = $count = 0; $_ = $log1 ; while (/ Sorting took (\ d +.\ d +) seconds / && $i ++ < 10) { $runtime += $1 ; $count ++; print "2: Match found : $ 1 -- $runtime -- $count \ n "; };

b. As you can see from the output, the code does not work as desired, it finds the same match again and again. Correct the code by adding the appropriate modifier to / /. Execute the corrected code. If your code is correct, the output will be 2: Match found : 10.486 -- 10.486 -- 1 2: Match found : 9.276 -- 19.762 -- 2

c. Extend your Perl script with code that computes and prints out the average runtime by dividing $runtime by $count. The output should look as follows: Average runtime : 9.

foreach ( @urls ) { print " URL : $_ \ n "; ( $scheme , $domain , $port , $path , $query , $fragment ) = (/(.)(.)(.)(.)(.)(.)/ g ); print " SCHEME : $scheme , DOMAIN : $domain , PORT : $port \ n "; print " PATH : $path \n "; print " QUERY : $query \ n "; print " FRAGMENT : $fragment \ n \ n "; }

b. Now change the regular expression in the code above so that it correctly separates the five components of a URL and using the sample URLs test that it works as expected. To find the right regular expression it might be useful to experiment with http://www.perlfect.com/articles/regextutor.shtml c. Add code that removes dot-segments from $path. Hint: Refer to the lecture notes for the substitution that is required to do so.

  1. Repeat some of the code examples that we have seen in the last two lectures.

a. Add the following code to your Perl script, save it, and execute it. $_ = " ab 11 cd 22 ef 33"; if (/\ d +/ g) { print "1: Match starts at $ -[0]: $ &\ n " } if (/[ a - z ]+/ g ) { print "2: Match starts at $ -[0]: $ &\ n " } if (/\ d +/ g) { print "3: Match starts at $ -[0]: $ &\ n " }

b. Check that the output is as expected. c. Add the following code to your Perl script, save it, and execute it. $_ = " ab 11 cd 22 ef 33"; if (/\ d +/ g) { print "4: Match starts at $ -[0]: $ &\ n " } if (/ ab / g ) { print "5: Match starts at $ -[0]: $ &\ n " } if (/\ d +/ g) { print "6: Match starts at $ -[0]: $ &\ n " }

d. Check that the output is as expected. e. Correct the code in 6c. above so that the output is 4: Match starts at 3: 11 6: Match starts at 9: 22

f. Add the following code to your Perl script, save it, and execute it. $_ = " Bart teases Lisa "; @keywords = (" bart " ," lisa " ," marge " ," L \ w +" ," t \ w +"); while ( $keyword = shift ( @keywords )) { print " Match found for $keyword : $ &\ n " if / $keyword / i ; }

g. Check that the output is Match found for bart : Bart Match found for lisa : Lisa Match found for t \ w +: teases

Why is there no match for "L\w+"?

h. Add the following code to your Perl script, save it, and execute it. $name = " Dr Ullrich Hustadt "; $name =∼^ s /( Mr | Ms | Mrs | Dr )?\ s *(\ w +)\ s +(\ w +)/\ U$3 \E , $2 /; print " $name \n ";

$name = " < fill in your name >"; $name =∼ s /( Mr | Ms | Mrs | Dr )?\ s *(\ w +)\ s +(\ w +)/\ U$3 \E , $2 /; print " $name \n ";

i. Add the following code to your Perl script, save it, and execute it. $text = " The temperature is 105 degrees Fahrenheit "; $text =∼^ s !(\ d +) degrees Fahrenheit! (( $1 -32)*5/9)." degrees Celsius "! e ; print " $text \n "; $text =∼ s !(\ d +.\ d +)! sprintf ("% d " , $1 +0.5)! e ; print " $text \n ";

j. Simplify the code in 6i. above so that the transformation from degrees Fahrenheit to degrees Celcius is done is a single step.

  1. The following exercise is about Perl subroutines.

a. Read http://perldoc.perl.org/perlsub.html

and the lecture notes to get an understanding of Perl subroutines. b. Add the following code to your Perl script, save it and execute it. sub sum { return $_ [0] + $_ [1]; }

$first = "3"; $second = 4; $result = & sum ( $first , $second ); print " The sum of $first and $second is $result \ n ";

c. Refine the sub routine above so that it can compute the sum of any list of numbers. Hint: The answer is in the lecture notes.

  1. The sum subroutine in 7 returns a single scalar value. Returning a list is just as simple, as the following exercise shows.

a. Add the following code to your Perl script, save it, and execute it. sub format_names { my @names = @_ ; foreach ( @names ) { s /( Mr | Ms | Mrs | Dr )?\ s *(\ w +)\ s +(\ w +)/\ U$3 \E , $2 / } return @names ; }