Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Operators: arithmetic and otherwise

by root (Monk)
on Nov 17, 1999 at 01:12 UTC ( [id://990]=perltutorial: print w/replies, xml ) Need Help??

The Basic Arithmetic Opertators

ExampleTypeResult
$a+$bAdditionSum of $a and $b
$a-$bSubtractionResult of $b subtracted from $a
$a*$bMultiplicationProduct of $a and $b
$a/$bDivisionResult of $a divided by $b
$a%$bModulusRemainder when $a is divided by $b
$a**$bExponentiation$a to the power of $b


String Operators
To prevent confusion strings have their own operators. Perl has its own addition and mulitply operators for strings. These operators are . and x respectively. We'll show you how these work compared to their arithmetic counterparts.
$a=2; $a=3; print $a+$b #arithmetic operator prints 5 print $a.$b #string operator prints 2 plus the three or 23 print $a*$b #arithmetic operator prints 6 print $a x $b #string operators prints $a $b times or 2 three times. i +e 222


Assignment Operators

Assignment simply set values on the left side of a = to what is on the right side. This works for both strings and numbers in Perl. You can speed an assignment like $a=$a*3; by using a handy shortcut used in C and C++. You can simplify variable=variable operator expression to variable operator=expression. We'll demonstrate this with some quick examples.
$a=3; $b="x"; $c=4; $a*=3; #$a=$a*3; $a now equal to 9; $a/=3; #$a=$a/3; $a (9) divided by three which equals 3; $a+=2; #$a=$a+2; $a is now equal to 5; $a-=2; #$a=$a-2; $a is now equal to 3; $b x=3; #$b=$b x $3 $b is now equal to "xxx"; $b .="33"; #b=$b."33" $b is now equal to "xxx33";

Another assignment operator often used is ||= which sets a variable equal to a value if the value isn't already set.

Comparison Operators
TypeNumericString
Greater Than>gt
Less Than<lt
Equal to==eq
Not equal!=ne
Less than or equal to<=le
Greater than or equal to>=ge

Another comparison operator is the <=> operator which returns -1 if the second term is greater, 1 if the first term is greater and 0 if the terms are equal. The string equivalent of <=> is cmp.

Autoincrement and Autodecrement operators These operators simply add or subtract one from a given variable. If the operator comes before the variable the value of the variable after the operation is returned. If the operation comes after the variable the value before the operation is returned. For example
$a=1; print $a++; #prints a as one then adds 1 to it print $a; #now $a is 2 print ++$a; #adds one to $a and then prints its value which is now 3; print $a--; #prints 3 then subtracts one from $a;


Logical Operators
ExamplesShort VersionTextual VersionMeaning
$a and $b; $a && b&&andreturns true if $a and $b are both defined and nonzero
$a or $b; $a||$b ||orreturns true if either $a or $b is defined and nonzero
!$a; not $a!notreturns the opposite of what an expression would otherwise


Note those operators are useful for controlling execution based on the way short-circuiting occurs. If you want something to happen only if the first condition isn't met you can use an or.

$a or print '$a is notdefined or is equal to zero';

You can also use an and to allow something to execute only if the first criteria evaluates to 0;


$isMonday and print "Today is Monday\n";


If you want to find ALL the information on ALL the operators here's your place

Replies are listed 'Best First'.
RE: Operators: arithmetic and otherwise
by Anonymous Monk on Apr 24, 2000 at 22:31 UTC
    $a=3; $b="x"; $c=4; $a*=3; #$a=$a*3; $a now equal to 9; $a/=3; #$a=$a/3; $a (9) divided by three which equals 3; $a+=2; #$a=$a+2; $a is now equal to 5; $a-=2; #$a=$a/2; $a is now equal to 3; $b x=3; #$b=$b x $3 $b is now equal to "xxx"; $b .="33"; #b=$b."33" $b is now equal to "xxx33";
    Typo in 7th line? Perhaps ought to be #$a=$a-2;
      Typo in 8th line. #$b=$b x 3 $b is now equal to "xxx";
Re: Operators: arithmetic and otherwise
by Anonymous Monk on Jan 12, 2001 at 21:10 UTC
    There's a typo in the section demoing the sting operators.

    Are we really wanting to set $a=2 AND $a=3 next line? don't you mean $b=3, especially since otherwise it's not set properly?

    :) :) :)

Re: Operators: arithmetic and otherwise
by Anonymous Monk on Jun 29, 2001 at 01:14 UTC
    "You can also use an and to allow something to execute only if the first criteria evaluates to 0; $isMonday and print "Today is Monday\n";" if the first criteria evaluates to 0, the followed command will not execuate. In this situation, an "or" should be used instead of an "and".
      Actually, it looks like it *should* be 'and'. But it should be "only if the first criteria evaluates to 1".
RE: Operators: arithmetic and otherwise
by Falthor (Initiate) on Jun 02, 2000 at 02:52 UTC
    $a=2; $a=3; print $a+$b #arithmetic operator prints 5 print $a.$b #string operator prints 2 plus the three or 23 print $a*$b #arithmetic operator prints 6 print $a x $b #string operators prints $a $b times or 2 three times. ie 222 Shouldn't there be end of lin characters on the end of lines 3-7 ?

    Falthor

Re: Operators: arithmetic and otherwise
by lvanhout (Curate) on Jun 15, 2004 at 03:10 UTC
    The second variable $a=3 should be $b=3 I believe:

    $a=2; $b=3;
    print $a+$b #arithmetic operator prints 5 print $a.$b #string operator prints 2 plus the three or 23 print $a*$b #arithmetic operator prints 6 print $a x $b #string operators prints $a $b times or 2 three times. i +e 222

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://990]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-19 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found