Skip to main content

$VECTORDEFINED (ObjectScript)

Determines if vector element at specified position is defined.

Synopsis

$VECTORDEFINED(vector,position)
$VECTORDEFINED(vector,position,value) 

Description

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)

See Also

FeedbackOpens in a new tab