perltutorial root <B>The Basic Arithmetic Opertators</B><BR><BR> <TABLE border=1> <TR><TD>Example</TD><TD>Type</TD><TD>Result</TD></TR> <TR><TD>\$a+\$b</TD><TD>Addition</TD><TD>Sum of \$a and \$b</TD></TR> <TR><TD>\$a-\$b</TD><TD>Subtraction</TD><TD>Result of \$b subtracted from \$a</TD></TR> <TR><TD>\$a*\$b</TD><TD>Multiplication</TD><TD>Product of \$a and \$b</TD></TR> <TR><TD>\$a/\$b</TD><TD>Division</TD><TD>Result of \$a divided by \$b</TD></TR> <TR><TD>\$a%\$b</TD><TD>Modulus</TD><TD>Remainder when \$a is divided by \$b</TD></TR> <TR><TD>\$a**\$b</TD><TD>Exponentiation</TD><TD>\$a to the power of \$b</TD></TR> </TABLE> <BR><BR> <B>String Operators</B><BR> 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.<BR> <PRE><CODE> \$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 </CODE></PRE> <BR> <BR> <B>Assignment Operators</B><BR><BR> 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 <I>variable=variable operator expression</I> to <I>variable operator=expression</I>. We'll demonstrate this with some quick examples.<BR> <PRE><CODE> \$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"; </CODE></PRE> <BR>Another assignment operator often used is ||= which sets a variable equal to a value if the value isn't already set.<BR> <BR> <B>Comparison Operators</B> <TABLE border=1> <TR><TD>Type</TD><TD>Numeric</TD><TD>String</TD></TR> <TR><TD>Greater Than</TD><TD>></TD><TD>gt</TD></TR> <TR><TD>Less Than</TD><TD><</TD><TD>lt</TD></TR> <TR><TD>Equal to</TD><TD>==</TD><TD>eq</TD></TR> <TR><TD>Not equal</TD><TD>!=</TD><TD>ne</TD></TR> <TR><TD>Less than or equal to</TD><TD><=</TD><TD>le</TD></TR> <TR><TD>Greater than or equal to</TD><TD>>=</TD><TD>ge</TD></TR> </TABLE> <BR>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.<BR><BR> <B>Autoincrement and Autodecrement operators</B> 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<Br> <PRE><CODE> \$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; </CODE></PRE> <BR> <BR> <B>Logical Operators</B> <TABLE BORDER=1> <TR><TD>Examples</TD><TD>Short Version</TD><TD>Textual Version</TD><TD>Meaning</TD></TR> <TR><TD>\$a and \$b; \$a && b</TD><TD>&&</TD><TD>and</TD><TD>returns true if \$a and \$b are both defined and nonzero</TD></TR> <TR><TD>\$a or \$b; \$a||\$b</TD> <TD>||</TD><TD>or</TD><TD>returns true if either \$a or \$b is defined and nonzero</TD></TR> <TR><TD>!\$a; not \$a</TD><TD>!</TD><TD>not</TD><TD>returns the opposite of what an expression would otherwise</TD></TR> </TABLE> <BR> <BR> 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. <BR><BR><code>\$a or print '\$a is notdefined or is equal to zero';</code><BR><BR> You can also use an and to allow something to execute only if the first criteria evaluates to 0; <BR> <BR><BR><code>\$isMonday and print "Today is Monday\n";</code><BR> <BR><BR>[perlman:perlop|If you want to find ALL the information on ALL the operators here's your place]