PERL Class Lesson 2
ORDER OF OPERATIONS SCRIPT

previous script, LEARNING BASIC MATHMATIC OPERATIONS SCRIPT              lesson2 overview              next script, MATH OPERATIONS SHORTCUTS SCRIPT



ORDER OF OPERATIONS


- vim orderofops.pl

Here we have 1 equation "1+2/3*4*25 is " set up 4 different ways,
some have parenthesis while others don't.

Try to figure out what each function does before running it




Change permissions so it will run (chmod 755 orderofops.pl)
Now run it and check if you were right (./orderofops.pl)





Here you see we have errors and strange answers to the equations.

First let us look at the equations

The first equation ran according to the program following the order of operations and moving to a new line for the next equation in the program
     - The program computed the equation by doing 2 divided by 3,
     - and that answer was multiplied by 4,
     - and that answer was multiplied by 25,
     - and that answer was added to by 1

The output was 67.66666667 and the curser moved to a new line

The second equation throws us for a loop
Notice how only what is in the ( ) is printed out
Everything else is ignored, even the newline command

The output was 3 and did not move the curser to a new line for the next equation

The third equation behaves the same as the second
Even with a ( ) elsewhere in the equation it ignores the rest of the line

Once again the output was 3 and did not move the curser to a new line for the next equation

The forth equation we got smart and incorperated everything within an all encompassing set of parenthesis
This tells the program to compute everything within the ( ) parenthesis
Anything within the ( ) parenthesis is now a set of operations which have been given an order to follow
     - The program computed the equation by doing 1 plus 2,
     - and that answer was divided by 3,
     - and that answer was multiplied by the product created by 4 multiplied by 25,

The output was 100 and moved the curser to a new line

So to get the second, and third equations to output 100 without any errors we need to change the code

The Errors:

print (...) interpreted as function at ./orderofops.pl line 19.
print (...) interpreted as function at ./orderofops.pl line 26.

means that the dot after the equation is telling the computer to do something more instead of telling the computer that the equation is done.

The Errors:

Useless use of concatenation (.) or string in void context at ./orderofops.pl line 19.
Useless use of concatenation (.) or string in void context at ./orderofops.pl line 26.

means that the computer is reading the dot that is after the equation to be something that joins the equation to something else in the program that is not specified and computer is unhappy about it.

So how do we get rid of these errors without sticking everything into one giant set of parenthesis?

Well, if we remove the dot at the end of the equation it will not know what to do as the computer will no longer recognize the equation to be an equation.
So that won't work.


- Go ahead and open the script again, vim orderofops.pl
- Follow along and fix the script so it will work properly





Run it and check if it works (./orderofops.pl)



Congratulations!!!! You did a great job

Your HOMEWORK is to play with this and print out your own  equations  and use various order of operations along with the concatenation function, and don't forget the  newline command 

==================================================

previous script, LEARNING BASIC MATHMATIC OPERATIONS SCRIPT              lesson2 overview              next script, MATH OPERATIONS SHORTCUTS SCRIPT