Perl Script Exercises: Understanding Strings, Numbers, and Control Structures, Lecture notes of Programming Languages

This perl script contains various exercises to help students understand the conversion between strings and numbers, different types of strings, and control structures such as if-then-else statements and switch statements. Topics like single and double quoted strings, auto-increment and auto-decrement operators, numeric and string comparison, and the use of given/when statements.

Typology: Lecture notes

2011/2012

Uploaded on 07/16/2012

sambandan
sambandan ๐Ÿ‡ฎ๐Ÿ‡ณ

4.7

(3)

35 documents

1 / 5

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 .
p ri nt " H e ll o W or l d \n " ;
b. Save that the code to a file named perlExercise 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 perlExercise is executable by using the command
chmod u+x perlExercise
e. Now execute the Perl script using the command
./perlExercise
and check that the output is:
Hello World!
2. We also looked at single quoted and double quoted strings and the different interpretation
of the backslash character in these.
a. Add the following code at the end of your Perl script, save the file and then execute it.
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Perl Script Exercises: Understanding Strings, Numbers, and Control Structures 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.

print " Hello World \ n ";

b. Save that the code to a file named perlExercise 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 perlExercise is executable by using the command chmod u+x perlExercise e. Now execute the Perl script using the command ./perlExercise and check that the output is: Hello World!

  1. We also looked at single quoted and double quoted strings and the different interpretation of the backslash character in these.

a. Add the following code at the end of your Perl script, save the file and then execute it.

$text = " stop !"; print โ€™ don \ โ€™ t \โ€™ don \ โ€™ t \ โ€™ " don \ โ€™ t " \ U$text โ€™ ,"\ n "; print " don โ€™ t โ€™don โ€™t โ€™ " don \ โ€™ t " \ U$text " ,"\ n \ n "; print โ€™ glass \ table glass \ table glass \ ntable โ€™ ,"\ n "; print " glass \ table glass \ table glass \ ntable " ,"\ n \ n ";

b. Check that the output is as you would expect. c. Add two print statements to your Perl script that produce the following output: โ€™ There โ€™ s no fun in Java .\"

One statement should use single quoted strings the other double quoted strings.

  1. In the lectures you have learned that Perl converts between strings and numbers depend- ing on the context.

a. Add the following code at the end of your Perl script, save the file and then execute it. $student_id = "200846369"; $staff_id = " E00481370 " ; print " student_id = $student_id ; staff_id = $staff_id \ n "; $student_id ++; $staff_id ++; print " student_id = $student_id ; staff_id = $staff_id \ n "; $student_id += 1; $staff_id += 1; print " student_id = $student_id ; staff_id = $staff_id \ n ";

b. Try to figure out what is going on. Hint: Consult http://perldoc.perl.org/perlop.html#Auto-increment-and-Auto-decrement

  1. In the lectures you have learned that Perl distinguishes between numeric comparison op- erators and string comparison operators.

a. Add the following code at the end of your Perl script if (35 == 35.0) { print ("35 is numerically equal to 35.0\ n ") } else { print ("35 is not numerically equal to 35.0\ n ") } if ( โ€™35 โ€™ eq โ€™35.0 โ€™) { print (" โ€™35 โ€™ is string equal to โ€™35.0 โ€™\ n ") } else { print (" โ€™35 โ€™ is not string equal to โ€™35.0 โ€™\ n ") } if ( โ€™35 โ€™ == โ€™35.0 โ€™) { print (" โ€™35 โ€™ is numerically equal to โ€™35.0 โ€™\ n ") } else { print (" โ€™35 โ€™ is not numerically equal to โ€™35.0 โ€™\ n ") } if (35 < 35.0) { print ("35 is numerically less than 35.0\ n ")

for ( $month =1; $month <=12; $month ++) {

< your code here >

}

Save the file and execute it. b. Is the output correct? c. What happens if you change the for-loop to for ( $month =0; $month <=12; $month ++) {

< your code here >

}

Does your code still work? If not, can you fix it?

  1. You have seen in the lectures that list literals are a convenient way to initialise arrays.

a. Add the following code at then end of your Perl script, save the file and execute it. @array1 = (1..10); @array2 = (10.0..20.0); @array3 = (20.5..30.5); @array4 = (" a ".." z "); print "\ @array1 = " , join (" " , @array1 ) ,"\ n "; print "\ @array2 = " , join (" " , @array2 ) ,"\ n "; print "\ @array3 = " , join (" " , @array3 ) ,"\ n "; print "\ @array4 = " , join (" " , @array4 ) ,"\ n ";

b. Try to make sense of the output. c. Add four print statements to your Perl script that print out the length of each of the four arrays. d. We have seen that Perl allows both positive array indices and negative array indices. What happens if those get out of bounds? e. Add the following code at the end of your Perl script, save the file and execute it. for ( $index =0; $index <=11; $index ++) { print "\ @array1 [ $index ] = " , $array1 [ $index ] ,".\ n "; } for ( $index = -1; $index >= -11; $index - -) { print "\ @array1 [ $index ] = " , $array1 [ $index ] ,".\ n "; } print "\ n ";

f. Check what happens if the index gets out of bounds. Try to find out what value is actually returned once the index gets out of bounds.

  1. Let us now use stack and queue operations on arrays.

a. Add the following code at the end of your Perl script, save the file and execute it. @planets = (" earth "); unshift ( @planets ," mercury " ," venus "); push ( @planets ," mars " ," jupiter " ," saturn "); print " Array \ @1 : " , join (" " , @planets ) ,"\ n "; $last = pop ( @planets );

print " Array \ @2 : " , join (" " , @planets ) ,"\ n "; $first = shift ( @planets ); print " Array \ @3 : " , join (" " , @planets ) ,"\ n "; print " \ @4 : " , $first , " " , $last , "\ n ";

b. Check that the output is as described in the lecture notes.

c. There are still four elements left in the array. What happens if we continue to use pop elements of the array? Add the following code at the end of your Perl script, save the file, then execute it. for ( $index =1; $index <7; $index ++) { $next = pop ( @planets ); print (" The next planet removed is $next .\ n "); }