INPUT /  OUTPUT /  LANGUAGE





@ operator

Field operator

Generates an element or a slice of elements from a vector, or a character or a piece of the text from a scalar holding text.

Applies to...

Vector fields and scalar fields holding text.

Sample usage

For a vector defined as <v:vector {A}, {B}, {C}, {D}, {E}, {F}, {G}>:

For a vector defined as <n:vector 10, 20, 30, 40>:

For a scalar defined as <s:scalar {Hello World!}>:

Behaviour

Takes one, two or three numbers:

Positions start at 1. Negative positions count back from the end, so -1 is the last.

On a vector, the operator generates the chosen elements, in order. Text that ends up next to other text joins into one piece of text, just as it does anywhere else, so a slice of text elements generates a single piece of text. Values that aren't text stay separate: for <m:vector {a}, {b}, 1, {c}, {d}>, <m.@ 1,5> generates three values, {ab}, 1, {cd}. To get text elements as separate values, pick each one: <x:vector <v.@ 2>, <v.@ 3>> holds two elements.

On a scalar holding text, the operator works on the characters of the text, and generates a piece of the text. A scalar holding a number isn't text: it counts as a single value, so only <k.@ 1> works on it. To pick out digits, store the number as text, as in <k:scalar {12345}>.

Naming the result creates a scalar for a single position and a vector for a range: <x:v.@ 2,4> creates a vector holding one element, {BCD}.

A position that doesn't exist generates invalid, and the console reports "index out of range" in the terminal (or "slice out of range" for a range). Position 0 doesn't exist.

A step of 0, or a step that runs the opposite way to the range, can never reach the end of the range, so it also generates invalid and reports "slice out of range": <v.@ 2,5,-1> and <v.@ 2,5,0> are both invalid.

The operator takes values, so it needs angle brackets of its own: <putLn <v.@ 2>>. Without them, the values would be read as part of the surrounding tag.

See also