Help Search Campus Map Directories Webmail Home Alumnae Academics Admission Athletics Student Life Offices & Services Library & Technology News & Events About the College Navigation Bar
MHC Home Using the Document Interface
TopNext Page

Arithmetic and Logic

It is often helpful to be able to display calculated values in an HTML file. And when using the .IF command, you will often want to have it act differently based on some logical condition.

The Document Interface supports a number of arithmetic and logic operators, all of which are actually handled by the Perl language. The operators themselves must not be within either type of quotes, even if the operands are.

Operations are normally evaluated using set rules of precedence. This means that, for instance, multiplication is carried out before addition. To override this, operations can be surrounded by any number of parentheses () to give them precedence. For example, the following operations produce the results on the right:

Operation

Result

Reason

2+3*4
(2+3)*4
2==2 || 1==2 && 3==3 || 3==4
(2==2 || 1==2) && (3==3 || 3==4)
14
20
false
true
multiplication * evaluated first

logical AND && evaluated first

The following table lists the operators in order of precedence, with the highest precedence listed at the top. Operations of equal precedence are always evaluated from left to right.

Numeric, String, and Logic Operators

Operator

Description

Example

Result

#string#expression# Returns the portion of string which matches the Perl regular expression, or an empty string if there is no match. For a full description of this advanced feature, see the Perl page on regular expressions. #abcdef#a[bc]#
#abcdef#a.*e#
ab
abcde
** Exponentiation 10**2
4**3
100
64
! Logical negation (Not) !(1==2)
!1 || !0
true
true
*
/
%
Multiplication
Division
Modulo
10*3 + 24/6
10*(3 + 24)/6
11/4
11%4
34
45
2.75
3
+
-
.
Addition
Subtraction
String concatenation
3+4
3-4
"abc"."ab"."z"
7
-1
abcabz
<
>
<=
>=
lt
gt
le
ge
Less than
Greater than
Less than or equal to
Greater than or equal to

Alphabetically less than
Alphabetically greater than
Alphabetically less than or equal to
Alphabetically greater than or equal to
1<2
1>2
1+2<=3
1+2>=4

"abc" lt "def"
"abc" ge "a"
"abc" le "abc"
"aBc" ge "abc"
true
false
true
false

true
true
true
false
==
!=
eq
ne
Equals
Does not equal

String equals
String does not equal
9*5==5
10/3!=3

"abc" eq "Abc"
"abc" ne "abcd"
false
true

false
true
&& Logical And 1+2==3 && 2>1 true
|| Logical Or 1+2==3 || 1>2
2==0 && 1==1 || 2==2
true
true

All of the string operators (lt, ge, ne, etc.) are case-sensitive. You have to enter them in lowercase.

When using logical expressions with the .IF command, you should keep in mind that the code contained within the .IF will not be evaluated if the argument passed to it is either the number 0, an empty string, or any of the strings "no", "off", or "false" (case does not matter.) This means that all of the following examples produce the same result:

<!--.if 0-->
<!--.if fALse-->
<!--.if 1==99 || 4+4==9-->

----------------------------------------
Top | Next Page

Home | Directories | Web Email | Calendar | Campus Map | Search | Help

About the College | Admission | Academics | Student Life | Athletics
Offices & Services | Giving | News & Events | Alumnae | Library & Technology

Copyright © 2001 Mount Holyoke College. This page created by the OIS Operations Group and maintained by Webmaster. Last modified on November 2, 2001.