



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
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




#!/ usr / bin / perl
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!
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.
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
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 ++) {
}
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 ++) {
}
Does your code still work? If not, can you fix it?
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.
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 "); }