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)