Chapter 15. Arithmetic Expansion
Arritmatic operators are means of executing basic arithmatic equations
inside of your scripts. Perl can perform addition, subration, multiplication,
division, exponents, or modulus remainders on numbers. Keep in mind though,
they are not used to combine strings. There are some special string operators
for this.
| Sample Code: | Output: |
#!/usr/bin/perl
$x = 81; #The variable
$add = $x + 9;
$sub = $x - 9;
$mul = $x * 10;
$div = $x / 9;
$exp = $x ** 5;
$mod = $x % 79;
print "$x plus 9 is $add\n";
print "$x minus 9 is $sub\n";
print "$x times 10 is $mul\n";
print "$x divided by 9 is $div\n";
print "$x to the 5th is $exp\n";
print "$x divided by 85 has a remainder of $mod\n";
|
81 plus 9 is 90
81 minus 9 is 72
81 times 10 is 810
81 divided by 9 is 9
81 to the 5th is 3486784401
81 divided by 79 has a remainder of 2
|
Arithmatic Operators
| Operator | Example | Result | Definition |
| + | 7 + 7 | = 14 | Addition |
| - | 7 - 7 | = 0 | Subtraction |
| * | 7 * 7 | = 49 | Multiplication |
| / | 7 / 7 | = 1 | Division |
| ** | 7 ** 7 | = 823543 | Exponents |
| % | 7 % 7 | = 0 | Remainder |
Assignment Operators:
Using assignment operators we can modify out variable ($x) using several of our
arithmatic operators from above. Each time we preform an operation, our
variable is reassigned with the new value from the assignment operator.
For example; $x+=10; is the same as $x=$x+10;
| Operator | Definition | Example |
| += | Addition | $x += 10; |
| -= | Subtraction | $x -= 10; |
| *= | Multiplecation | $x *= 10; |
| /= | Division | $x /= 10; |
| %= | Modulus | $x %= 10; |
| **= | Exponent | $x **= 10; |
Logical & Relational Operators
Relationship operators relate one variable to another, 5 < 12 for example.
They are used to relate equallity or inequallity of two or more variables, be
it a string or numeric data
Logical operators state and/or relationships. Meaning, you can take two
variables and test an either or conditional. Logical operators are used later
on in conditionals and loops. For now, just be able to recognize them in the
upcoming examples.
Relational
| Operator | Example | Defined | Result |
| ==,eq | 5 == 5 5 eq 5 | Test: Is 5 equal to 5? | True |
| !=,ne | 7 != 2 7 ne 2 | Test: Is 7 not equal to 2? | True |
| <,lt | 7 < 4 7 lt 4 | Test: Is 7 less than 4? | False |
| >,gt | 7 > 4 7 gt 4 | Test: Is 7 greater than 4? | True |
| <=,le | 7 <= 11 7 le 11 | Test: Is 7 less than or equal to 11? | True |
| >=,ge | 7 >= 11 7 ge 11 | Test: Is 7 greater than or equal to 11? | False |
Logical
| Operator | Defined | Example |
| &&,and | Associates two variables using AND | if (($x && $y) == 5)... |
| ||,or | Associates two variables using OR | if (($x || $y) == 5)... |
Logic operators may be review for anybody with a background in programming.
They are fairly universal in nature. If they are new to you, remember what
they stand for so you can translate code.
Rounding Floating-Point Numbers
You want to round a floating-point value to a certain number of decimal places.
Use sprintf or printf Perl functions.
| Sample Code: | Output: |
#!/usr/bin/perl
$a = 0.255;
$b = sprintf("%.2f", $a);
print "Unrounded: $a\nRounded: $b\n";
printf "Unrounded: $a\nRounded: %.2f\n", $a;
| Unrounded: 0.255
Rounded: 0.26
Unrounded: 0.255
Rounded: 0.26
|
|