next up previous 405
Next: Logical
Up: Arithmetic
Previous: Arithmetic


Integer

You may include integer arithmetic in your scripts. First you must assign a value to each variable used with the set command.

     set count = 1
     set data = (10 24 30 17)
     set exception = -999
     set list

Notice that arrays are defined as spaced-separated values in parentheses. So $data[3] is 30. You can also set a variable to a null value, such as list above, so that the variable is ready to be assigned to an expression.

To perform arithmetic the expression is prefixed with the special @ character like this.

     @ count = $count + 1
     @ count++
Both of these add one to the value of count. Note that the space following the @ and around the = and + operator are needed. Likewise the examples below both subtract one from the value of count.

    @ count = $count - 1
    @ count--

There are several other operators, the most important ones are illustrated below.

     @ ratio = $count / 5
     @ octrem = $data[2] % 8
     @ numelement = $i * $j
The first divides the value of count by 5, rounding down, so if count was 11, ratio would be 2. The second assigns the remainder of the second element of the array called data after division by 8. So if data[2] is 30, octrem would be set to 6. Finally numelement is equated to the product of variables i and j.

The precedence order is * / %   followed by + -. If you are in doubt, use parentheses to achieve the desired order.



next up previous 405
Next: Logical
Up: Arithmetic
Previous: Arithmetic

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:ussc@star.rl.ac.uk

Copyright © 2009 Science and Technology Facilities Council