Cobra Inline Programs variables


variables and variable assignments


Variable names can be introduced at any place in an inline program, and they do not need to be declared. The type of a variable (which can be one of three different types: a string, an integer value, or a token reference) is deduced from assignments to that variable, and can therefore change from one assignment to the next.

Variable Scope

There are only two levels of scope in inline programs: global and function local, depending on the first time that the variable is encountered.
  • This means that if a variable name is first seen inside a function call, that instance will be function local. When the same name is later also seen outside all functions, this will create a new global instance of the variable. If now the same function from before is called again, the global instance of the variable will now be used.
  • To avoid potential confusion, variables (and associative arrays) can be declared with global scope with the notation:
    	global varname;		# declare varname as a global scalar variable
    	global arrayname[];	# declare arrayname as a global associative array
    But, make sure that this is the first occurrence of the name that is encountered in a script execution.


Assignment statements are written with a single equals sign:
	lhs = expr;
The left-hand side (lhs) can be a reference to a token (e.g., .) or a token field (e.g., .mark), a variable, or an element of an associative array.
Some examples of each use are:
	.mark = 5;	# mark is the only integer token field that can be assigned to
	.mark--;	# post decrement and increment are defined
	.mark++;	# as you suspected, this is a comment
	.txt = "Foo";	# .txt is one of two text fields that can be assigned to
	.typ = q.txt + .typ;	# .typ is the other; + is used here for string catenation
	. = .nxt;	# at the end of the complete token sequence, this will be a no-op
			# to check, one can do: if (. == .nxt), or better: if (. == End)
	. = .prv;	# at start of the complete token sequencem this is also a no-op
	. = .jmp;	# results in a reference to a dummy null-token if .jmp isnt set
			# this can be recognized by checking the .seq field
			# which will then be 0
	q = .;
	q = .nxt;
	. = q;
	. =;
	A[.txt] = .len;	# associative array assignment, to correlate a string with a value
	val = .lnr;

The + operator for Strings

On the right-hand side of an assignment, strings can be catenated with the plus operator (+).
This is also optional for string arguments in print statements. Therefore
		name = .fnm + " " + .typ;
		print name "\n";
has the same effect as:
		print .fnm " " .typ "\n";
and as typing:
		print .fnm + " " + .typ + "\n";
But the shorthand, without the plus, doesn't work for assignments, so typing
		name = .fnm " " .typ;	# syntax error
will give a syntax error;


The global keyword requires Cobra Version 3.2.

Inline Programs
(Last Updated: 13 November 2020)