Tinderbox v9 Icon

... (range)


Operator Type: 

Operator Scope of Action: 

Operator Purpose: 

Operator First Added: 

Operator Altered: 

 Operator   [other Operator type actions]

 Item   [operators of similar scope]

 Mathematical   [other Mathematical operators]

 9.1.0

 


...

The range operator constructs a list of numbers from a specified starting point to a specified end point. Note: the range operator is written as 3 dots and not an ellipsis character. White space immediately before or after the operator is ignored as with other mathematical operators. For example:

1…3 → "1;2;3"

3 … 1 → "3;2;1"

The range operator binds more tightly than arithmetic operators. Thus

1...3 * 2 → "2;4;6"

and is the is the same as

(1..3) * 2 → "2;4;6" 

i.e. the parentheses in the second example are not needed.

The range operator can be useful for performing a task a specific number of times using the List.each() operator:

1...10.each(x){var vPath="/container/item "+x; create(vPath);} 

and in that example the value of the loop variable x is the value for the source list generated by the range operator, i.e. it is the same as would occur in this literal example:

"1;2;3;4;5;6;7;8;9;10".each(x){var vPath="/container/item "+x; create(vPath);} 

In both cases, on the second iteration of the .each() loop, the loop variable x would have the value 2.

Using non-literal range specifiers

It may be useful to define one or both range limits. For example, if $MyNumber is 3:

$MyList = 1 ... $MyNumber; gives "1;2;3"

or if variable vNum is 4:

$MyList = 1 ... vNum; gives "1;2;3;5"

But for more complex expressions using chained operators, parentheses my be needed to hint intent to Tinderbox. Thus, if $MySet has 4 items:

$MyList = 1 ... $MySet.size; gives "1" WRONG

so add parentheses around the expression:

$MyList = 1 ... ($MySet.size); gives "1;2;3;4" CORRECT

If chaining operators to range specified using expressions, consider using parentheses around the whole range definition:

(1 ... ($MySet.size)).each(x){$Text+= x+"\n";}; 

The last example actually works without the parentheses but illustrates the concept.

Using range to supply a loop counter

When working with List.each(), it can be useful to know which source list item is being currently in scope. It is possible to use a variable (see here), but the range operator offers another method. In this method a range-generated list is iterated and in-loop the list being processed is called via List.at(). It in important to note that as .at() numbers from zero, either the range must start from zero, or the range item used with at must be adjusted by -1.

In the following example, $MyList holds the data of interest, and a zero-based range with be generated:

	(0 ... ($MyList.count)).each(N){
		$MyList.at(N) ... etc.
	}

Thus for loop #3, the value of N will be 2 (recall the range code is making a zero based list of numbers). Thus in-loop, $MyList.at(N) will be the same as $MyList.at(2), i.e. addressing the the third item in $MyList.