Perl Arithmetic Operators
Perl Operators – Part 2
Perl Course
Foreword: In this part of the series I talk about Arithmetic Operators in Perl.
By: Chrysanthus Date Published: 12 Oct 2015
Introduction
Additive Operators
Addition Operator
Read and try the following code. The explanation is given below:
use strict;
my $id1 = 20;
my $id2 = 30;
my $id3 = $id2 + $id1;
print $id3;
20 is held in the variable, $id1. 30 is held in the variable, $id2. In the third statement of interest, Perl takes the content of $id2 and adds it to the content of $id1, then it places the result as content for the variable of the newly declared variable, $id3. This addition is done without affecting or changing the contents of $id2 and $id1. The addition is done in a different area of memory.
Subtraction Operator
Read and try the following code. The explanation is given below:
Code example:
use strict;
my $id1 = 20;
my $id2 = 30;
my $id3 = $id2 - $id1;
print $id3;
20 is held in the variable, $id1. 30 is held in the variable, $id2. In the third statement of interest, Perl takes the content of $id2 and subtracts from it, the content of $id1, then it places the result as content for the variable of the newly declared variable, $id3. This subtraction is done without affecting or changing the contents of $id2 and $id1. The subtraction is done in a different area of memory.
The negation operator is the minus sign, it changes a positive number to a negative number. Consider the following statement:
my $var = -5;
5 is by definition a positive number. The negation operator on the right argument of the ordinary assignment operator, turns the 5 to a negative number. The negative number is stored in $var.
The Dot Operator
The dot (.) is an additive operator, but it is not really used for math (arithmetic); it is used to concatenate strings. Read and try the following program that illustrates this:
use strict;
my $str1 = "one one";
my $str2 = " two two";
my $str3 = " three three";
my $str = $str1 . $str2 . $str3;
print $str;
There are three small strings ($str1, $str2, $str3) in the program. They are joined (concatenated) together with the dot operator to form the string, str.
Multiplicative Operators
Read and try the following code. The explanation is given below:
use strict;
my $id1 = 20;
my $id2 = 30;
my $id3 = $id2 * $id1;
print $id3;
Note that the multiplication operator is * and not X. 20 is held in the variable, $id1. 30 is held in the variable, $id2. In the third statement of interest, Perl takes the content of $id2 and multiplies it with the content of $id1, then it places the result as content for the newly declared variable, $id3. This multiplication is done without affecting or changing the contents of $id2 and $id1. The multiplication is done in a different area of memory.
Division Operator
Read and try the following code. The explanation is given below:
Code example:
use strict;
my $id1 = 3;
my $id2 = 15;
my $id3 = $id2 / $id1;
print $id3;
Note that the division operator is, / . 3 is held in the variable, $id1. 15 is held in the variable, $id2. In the third statement of interest, Perl takes the content of $id2 and divides it by the content of $id1, then it places the result as content for the newly declared variable, $id3. This multiplication is done without affecting or changing the contents of $id2 and $id1. The multiplication is done in a different area of memory.
Read and try the following code. The explanation is given below:
Code example:
use strict;
my $id1 = 3;
my $id2 = 15;
my $id3 = $id2 / $id1;
print $id3;
Note that the division operator is, / . 3 is held in the variable, $id1. 15 is held in the variable, $id2. In the third statement of interest, Perl takes the content of $id2 and divides it by the content of $id1, then it places the result as content for the newly declared variable, $id3. This multiplication is done without affecting or changing the contents of $id2 and $id1. The multiplication is done in a different area of memory.
Modulus Operator
The modulus operator divides the first argument by the second argument and returns the remainder. Read and try the following code:
use strict;
my $id1 = 17;
my $id2 = 12;
my $id3 = $id1 % $id2;
print $id3;
The Modulus operator is the percentage sign. For the third statement of interest, Perl takes the content of $id1 and divides it by the content of $id2. The remainder of the division is placed in the variable, $id3. The division is done in a different area of memory.
The repetition operator is x. It takes two arguments: one on the left and one on the right. The one on the right is a number. The argument on the left can be a character, string or list.
The operator repeats (printing) what is on the left the number of times of the number on the right. The operator can work with the print statement to send the repetition to the browser or console.
Read and try the following program:
use strict;
print '-' x 80, "\n"; # prints row of dashes
print "\t" x 3, "text text", "\n" ; #prints 3 tabs and then "text text" on same line
The printed tabs (\t) effects will not appear at the browser but will appear at the console.
Read and try the following program for a list of strings:
use strict;
my @arr = ("one ", "two ", "three ") x 4;
print @arr;
The list is in parentheses. There are three items in the list. The repetition is in groups of the three items. The array ends up with 12 items.
Exponentiation
The exponentiation operator is not really an arithmetic operator. I have just included it in this tutorial for convenience. The operator is **. It is used to type exponentials: something like “4 raised to the power 2” is written as 4**2. The result of 4**2 is 16, that is, 4 X 4 = 16. Read and try the following code:
use strict;
my $result = 4**2;
print $result;
Apart from the negation and exponentiation operator, each arithmetic operator operates from left to right; that is from the left argument to the right argument. The negation and exponentiation operators operate in the opposite direction. The negation operator is a unary operator.
Left Associativity
If you have consecutive arithmetic operators, the one on the left will be evaluated first, followed by the one next to it coming to the right, followed by the one next to it, rightward, and so on. Consider the following code:
use strict;
my $ret = 9 - 5 - 2;
print $ret;
The output is:
2
The output is 2 because 9-5 is evaluated first before the result of 4 is used to evaluate 4-2. If 5-2 had been evaluated first, before 9-3, the output would have been 6.
The exponentiation operator is right associative, while the arithmetic operators are left associative.
That is it for arithmetic operators. We take a break here and continue in the next part of the series.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT