



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




#!/ usr / bin / perl
$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
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.
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.
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.
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.
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 ; }