INCLAN: Variable substitutions: Difference between revisions

From CYANA Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
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
  do i 1 nparam
    print "Parameter $i: $p$i" # $p$i inserts the value of the i-th command line
    print "Parameter $i: $p$i"   # $p$i inserts the value of the i-th command line
# parameter (p1, p2,…).
                                  # parameter (p1, p2,…).
  end do
  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 ==


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.
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)"
  x:=4.6; y:=2.0; sum=x+y
4.6 + 2.0 =    0.660E+01
  print "$x + $y = $sum(E12.3)"
  4.6 + 2.0 =    0.660E+01


== Substring ==
== 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.
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)"
t:=a sum
another sum
print "another $t(3:5)"
t(3:):=program # Assignment to a substring
another sum
print "$t"
t(3:):=program # Assignment to a substring
a program
print "$t"
a program


== List element ==
== 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.  
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
s:=17,28,,56,"This is the end"
  print "Element $i: $s(i)"
do i 1 length(’s’) # Length returns the number of elements
end do
  print "Element $i: $s(i)"
Element 1: 17
end do
Element 2: 28
Element 1: 17
Element 3:
Element 2: 28
Element 4: 56
Element 3:
Element 5: This is the end
Element 4: 56
Element 5: This is the end


== Function call ==
== 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.
'''$'''''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  
x=2.5; print "log(x)= $log(x)"
log(x) = 0.916291


== Expression ==
== Expression ==


“${expression}” or “%{expression}” substitute with the result value of an 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
x=2.5; y=10.0; print "x/y = ${x/y}"
x/y = 0.25

Latest revision as of 16:00, 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