Answered step by step
Verified Expert Solution
Question
1 Approved Answer
~ Perl Assignment 02 ~ Objectives To get practice in using the Perl language by using common programming concepts: Program arguments, arrays, and subroutines Create
~ Perl Assignment 02 ~
- Objectives
- To get practice in using the Perl language by using common programming concepts:
- Program arguments, arrays, and subroutines
- Create a Perl script that defines subroutines that do various tasks given two integers entered as program arguments.
- Subroutine #1: Print consecutive numbers from the first integer to the second integer
- Subroutine #2: Calculate the summation from the first integer to the second integer and return the result.
- Subroutine #3: Perform the SpamMusubi algorithm from the first integer to the second and return the result as a string.
- To get practice in using the Perl language by using common programming concepts:
- Perl Program 02
- Download the start file LastnameFirstname02.pl and ensure it is in the same folder that you are using to save all your Perl scripts.
- Right-click on the link above and Save link as to download the file.
- Rename the file so that Lastname and Firstname are your own last name and first name.
- Open LastnameFirstname02.pl in Atom and inspect it. There are essentially 6 sections:
- A section for the program header where the description and author information will go.
- A section for program argument check(s).
- A section that calls the subroutines.
- A section to define the subroutines.
- When running this script, the user is expected to enter exactly 2 integers as program arguments. You do not have to validate that they are integers, we will learn to deal with that later with regular expressions. To ensure the subroutines work correctly, implement the following three program argument checks.
- For the first program argument check, ensure there are exactly 2 program arguments. Terminate the script with an appropriate error and usage message if there is not exactly 2 program arguments.
- See the error and usage messages in the Example Outputs on the bottom. You may adapt the error and usage message for your own scripts.
- For the second program argument check, ensure the first argument is greater than 0. Terminate the script with an appropriate error message if the first argument is not greater than 0.
- For the third program argument check, ensure the first argument is strictly less than the second. Terminate the script with an appropriate error message if the first integer is not strictly less than the second.
- Ensure the program argument checks are working before proceeding.
- Define a subroutine named countFirstToSecond. Given two integers as arguments, this subroutine will print each integer from the first to the second.
- Inside the subroutine, start by retrieving the two arguments from the default array and store them in variables. You can use array index notation, shift, or pop to do so. As an example, to retrieve the first subroutine argument using array index notation, it would look like this: my $firstInt = $_[0];
- Use a loop to count from the first number to the second and print each number.
- You can use the following code to call the countFirstToSecond subroutine: call countFirstToSecond Copy and paste the code into the call subroutine section.
- Be sure the countFirstToSecond subroutine works before proceeding.
- Define a second subroutine named sumFirstToSecond. Given two integers as arguments, this subroutine will calculate the summation from the first integer to the second integer and return the result. This subroutine will return the summation, meaning there will be no print statements in this subroutine.
- Inside the subroutine, start by retrieving the two arguments from the default array and store them in variables.
- Declare and initialize a variable named $summation. This variable will be used to calculate the summation and it will be returned at the end of the subroutine.
- Use a loop to count from the first number to the second.
- Calculate the summation by adding the loop increment variable to the summation variable inside the loop. It should look like this: $sum = $sum + $i;
- After the loop, the summation is done, return the summation variable.
- You can use the following code to call your sumFirstToSecond subroutine: call sumFirstToSecond Copy and paste the code into the call subroutine section.
- Be sure the sumFirstToSecond subroutine works before proceeding.
- Define a third subroutine named spamMusubiFirstToSecond. Given two integers as arguments, this subroutine will create a string containing the result of the SpamMusubi algorithm from the first integer to the second integer and return the result. This subroutine will return a string that represents the output of the SpamMusubi algorithm. There will be no print statements in this subroutine.
- Inside the subroutine, start by retrieving the two arguments from the default array and store them in variables.
- Declare and initialize a variable named $output. This variable will be used to build the string containing the output of the SpamMusubi alrogithm. Building a string means to concatenate on to what is already stored instead of overwriting it.
- Example: my $output = "";
- Perform the SpamMusubi algorithm starting from the first integer to the second integer. Instead of printing, concatenate on to the $output variable each word or number. The subroutine is building a string of the SpamMusubi output instead of printing.
- For numbers divisible only by 3, concatenate "Spam" on to $output.
- For numbers divisible only by 7, concatenate "Musubi" on to $output.
- For numbers divisible by 3 and 7, concatenate "SpamMusubi" on to $output.
- All other numbers, just concatenate on to $output.
- Be sure you are concatenating each word or number on to $output instead of overwriting it.
- After the loop, return $output.
- You can use the following code to call your spamMusubiFirstToSecond subroutine: call spamMusubiFirstToSecond Copy and paste the code into the call subroutine section.
- It is okay if your program crashes/produces warnings when the user enter letters or symbols instead of a number, the script is not designed to handle that input.
- Ensure that your code is sufficiently styled and documented.
- Fill in the program description and author information at the top.
- All code that you created is commented.
- Ensure each subroutine has a description as well as in-line comments.
- Styled meaning code is indented when applicable, variables are named properly, etc.
- See posted examples in the Lecture Material page.
- Download the start file LastnameFirstname02.pl and ensure it is in the same folder that you are using to save all your Perl scripts.
- Example Outputs The following outputs are all from the same script but run with different program arguments.
> perl MeyerEdward02.pl Error: MeyerEdward02.pl expects exactly 2 integer program arguments. Usage: perl MeyerEdward02.pl int1 int2 > perl MeyerEdward02.pl 1 Error: MeyerEdward02.pl expects exactly 2 integer program arguments. Usage: perl MeyerEdward02.pl int1 int2 > perl MeyerEdward02.pl 1 2 3 4 5 Error: MeyerEdward02.pl expects exactly 2 integer program arguments. Usage: perl MeyerEdward02.pl int1 int2 > perl MeyerEdward02.pl 0 5 Error: First integer argument 0 is not greater than 0. > perl MeyerEdward02.pl 6 5 Error: First integer argument 6 is not less than the second argument 5. > perl MeyerEdward02.pl 2 2 Error: First integer argument 2 is not less than the second argument 2. > perl MeyerEdward02.pl 1 21 Counting from 1 to 21 is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 The summation from 1 to 21 is: 231 The SpamMusubi from 1 to 21 is: 1 2 Spam 4 5 Spam Musubi 8 Spam 10 11 Spam 13 Musubi Spam 16 17 Spam 19 20 SpamMusubi Program done. > perl MeyerEdward02.pl 85 100 Counting from 85 to 100 is: 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 The summation from 85 to 100 is: 1480 The SpamMusubi from 85 to 100 is: 85 86 Spam 88 89 Spam Musubi 92 Spam 94 95 Spam 97 Musubi Spam 100 Program done.
- Hints/Notes
- Below are how many lines each subroutine is in my solution without comments. This is just to give you an idea how long each subroutine is roughly, exact number may vary.
- 1st subroutine: 5 lines
- 2nd subroutine: 7 lines
- 3rd subroutine: 20 lines
- For the 2nd subroutine, a Java implementation of summation can be found on slide 77 of this lecture from my ICS 111 class. You will need to adapt it to Perl syntax.
- In the 3rd subroutine, you are concatenating each word or number on to the $output variable. Without concantenate, you will be overwritting the entire contents of $output at each iteration - that is not what you want.
- For example, given the following variable: my $output = "";
- This line will overwrite the contents of $output: $output = "vanilla";
- Whereas, in this line, "vanilla" is concatenated on to $output, preserving what is already stored in $output: $output = $output . "vanilla";
- Below are how many lines each subroutine is in my solution without comments. This is just to give you an idea how long each subroutine is roughly, exact number may vary.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started