$vectordefined (ObjectScript)
Synopsis
$vectordefined(vector,position)
$vectordefined(vector,position,value)
Description
-
$vectordefined(vector,position) returns 1 (true) if the vector element at the specified index position is defined. If the element is undefined, the function returns 0 (false).
-
$vectordefined(vector,position,value) also stores the value of the vector element (if defined) in a local variable, value.
Use $vectordefined to check if vector element contain data, similar to how you can use the $data function to check if a variable contains data.
Abbreviated Form: $vd |
Arguments
vector
Global or local variable specifying the input vector.
-
If vector is not a vector ($isvector(vector) = 0), then $vectordefined raises a <VECTOR> error.
-
If vector is undefined or holds an empty string (""), then $vectordefined returns 0 and the value variable (if specified) is set to the empty string.
position
Positive integer specifying the vector element position to check. If position is less than 1, then $vectordefined raises a <VECTOR> error.
value
Local variable that stores the value of the vector element located at position.
-
If the element is defined ($vectordefined returns 1), then value is set to the element value.
-
If the element is undefined ($vectordefined returns 0), then value is set to the empty string ("").
If the value variable did not previously exist, then $vectordefined creates it.
Examples
Check Whether Vector Elements are Defined
Define a length-10 vector of random integers from 1 to 100, defining every other element only. Display the vector. The odd-numbered element positions do not contain values, as indicated by the consecutive commas. Your element values will vary.
for i=2:2:10 set $vector(vector,i,"integer") = $random(100)+1
zwrite vector
vector={"type":"integer", "count":5, "length":10, "vector":[,46,,67,,45,,82,,2]} ; <VECTOR> |
Check whether the even-numbered elements are defined. These $vectordefined calls all return 1.
write $vectordefined(vector,2)
write $vectordefined(vector,4)
write $vectordefined(vector,6)
write $vectordefined(vector,8)
write $vectordefined(vector,10)
Check whether the odd-numbered elements are defined. These $vectordefined calls all return 0.
write $vectordefined(vector,1)
write $vectordefined(vector,3)
write $vectordefined(vector,5)
write $vectordefined(vector,7)
write $vectordefined(vector,9)
Store Vector Elements In Variables
Define a 10-element vector, where the elements correspond to the first 10 letters of the alphabet. Display the vector.
for i=1:1:10 set $vector(vector,i,"string") = $extract("abcdefghij",i)
zwrite vec
vector={"type":"string", "count":10, "length":10, "vector":["a","b","c","d","e","f","g","h","i","j"]} ; <VECTOR> |
Store the first and last letters of the vector into local variables. These variables have the values "a" and "j", respectively.
write $vectordefined(vector,1,firstLetter)
write $vectordefined(vector,10,lastLetter)
zwrite firstLetter, lastLetter
Try to store a value at a position that is beyond the length of the vector. Because the value at this position is undefined, the variable is set to the empty string.
write $vectordefined(vector,26,undefinedLetter)
zwrite undefinedLetter
Alternatively, you can store vector elements in variables by using the $vector function.
set firstLetter = $vector(vector,1)
set lastLetter = $vector(vector,10)
set undefinedLetter = $vector(vector,26)