Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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)

37 documents

1 / 5

Related documents


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! 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 $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. 3. 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 4. 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") 2 docsity.com
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved