INCLAN: Variable substitutions: Difference between revisions
Line 3: | Line 3: | ||
== Basic substitutions== | == Basic substitutions== | ||
Substitutions of the form $variable or %variable insert the complete value of the variable (without trailing blanks) into the command line. Substitutions with “$” differ from those with “%” only if the value of the variable starts and ends with single quotes, i.e. if it is a FORTRAN-77 character constant: with “%” the delimiting single quotes are retained in the substitution, with “$” they are removed. A variable name that is immediately followed by a letter, digit, or underscore character must be enclosed in curly braces: “{$variable}”. | Substitutions of the form '''$'''''variable'' or '''%'''''variable'' insert the complete value of the variable (without trailing blanks) into the command line. Substitutions with “'''$'''” differ from those with “'''%'''” only if the value of the variable starts and ends with single quotes, i.e. if it is a FORTRAN-77 character constant: with “'''%'''” the delimiting single quotes are retained in the substitution, with “'''$'''” they are removed. A variable name that is immediately followed by a letter, digit, or underscore character must be enclosed in curly braces: “{'''$'''''variable''}”. | ||
x:=4.6; y:=2.0; sum=x+y; t:=a sum # set variables | x:=4.6; y:=2.0; sum=x+y; t:=a sum # set variables | ||
print "This is $t: $x + $y = $sum" # substitute values | print "This is $t: $x + $y = $sum" # substitute values | ||
This is a sum: 4.6 + 2.0 = 6.60000 | This is a sum: 4.6 + 2.0 = 6.60000 | ||
s:=’$t’ # create a Fortran string from a normal variable | s:=’$t’ # create a Fortran string from a normal variable | ||
print "\$s = $s; \%s = %s" # with and without single quotes | print "\$s = $s; \%s = %s" # with and without single quotes | ||
$s = a sum, %s = ’a sum’ | $s = a sum, %s = ’a sum’ | ||
print "{$t}mer" | print "{$t}mer" | ||
a summer | a summer | ||
All substitutions in the command line proceed from right to left. This allows, for example, composition of a variable name from the values of other variables before it is used in a substitution: | All substitutions in the command line proceed from right to left. This allows, for example, composition of a variable name from the values of other variables before it is used in a substitution: | ||
command list_param # user-defined command list_param | command list_param # user-defined command list_param | ||
do i 1 nparam | |||
print "Parameter $i: $p$i" # $p$i inserts the value of the i-th command line | |||
# parameter (p1, p2,…). | |||
end do | |||
end | end | ||
list_param 17 second last # call list_param | list_param 17 second last # call list_param | ||
Parameter 1: 17 | Parameter 1: 17 | ||
Parameter 2: second | Parameter 2: second | ||
Parameter 3: last | Parameter 3: last | ||
== Fortran format == | == Fortran format == |
Revision as of 14:47, 17 August 2009
There are several ways to insert the value of a variable or the result value of an expression into the command line:
Basic substitutions
Substitutions of the form $variable or %variable insert the complete value of the variable (without trailing blanks) into the command line. Substitutions with “$” differ from those with “%” only if the value of the variable starts and ends with single quotes, i.e. if it is a FORTRAN-77 character constant: with “%” the delimiting single quotes are retained in the substitution, with “$” they are removed. A variable name that is immediately followed by a letter, digit, or underscore character must be enclosed in curly braces: “{$variable}”.
x:=4.6; y:=2.0; sum=x+y; t:=a sum # set variables print "This is $t: $x + $y = $sum" # substitute values This is a sum: 4.6 + 2.0 = 6.60000 s:=’$t’ # create a Fortran string from a normal variable print "\$s = $s; \%s = %s" # with and without single quotes $s = a sum, %s = ’a sum’ print "{$t}mer" a summer
All substitutions in the command line proceed from right to left. This allows, for example, composition of a variable name from the values of other variables before it is used in a substitution:
command list_param # user-defined command list_param do i 1 nparam print "Parameter $i: $p$i" # $p$i inserts the value of the i-th command line # parameter (p1, p2,…). end do end list_param 17 second last # call list_param Parameter 1: 17 Parameter 2: second Parameter 3: last
Fortran format
Substitutions of the form $variable(format) or %variable(format) are used to format integer or real values of variables according to a FORTRAN-77 format. A format that contains the letter “I” or “i” applies to integer numbers, all other formats to real numbers.
x:=4.6; y:=2.0; sum=x+y print "$x + $y = $sum(E12.3)"
4.6 + 2.0 = 0.660E+01
Substring
Substitutions of the form $variable(n:m) or %variable(n:m), where n and m are positive integer expressions, are used to substitute with the substring between character positions n and m of the value of a variable. Substring expressions can also appear on the left hand side of assignment statements. t:=a sum print "another $t(3:5)" another sum t(3:):=program # Assignment to a substring print "$t" a program
List element
If the value of a variable is a comma-separated list, “$variable(n)” or “%variable(n)”, where n is a positive integer expression, substitute with the n-th element of this list. s:=17,28,,56,"This is the end" do i 1 length(’s’) # Length returns the number of elements
print "Element $i: $s(i)"
end do Element 1: 17 Element 2: 28 Element 3: Element 4: 56 Element 5: This is the end
Function call
“$function” or “%function” substitute with the result value of a function without parameters, “$function(parameters)” or “%function(parameters)” substitute with the result value of a function with parameters. If there are several parameters, they are separated by commas. x=2.5; print "log(x)= $log(x)" log(x) = 0.916291
Expression
“${expression}” or “%{expression}” substitute with the result value of an expression. x=2.5; y=10.0; print "x/y = ${x/y}" x/y = 0.25