Create Vector and Update Vector Elements
Create a three-element string vector and display the vector contents by using the zwrite command. The vector type is set to "string" and cannot be changed after you create the vector. The element count and vector length are both set to 3.
set $vector(v,1,"string") = "a"
set $vector(v,2,"string") = "b"
set $vector(v,3,"string") = "c"
zwrite v
v={"type":"string", "count":3, "length":3, "vector":["a","b","c"]} ; <VECTOR> |
Remove the second element from the vector. The element count decreases to 2. The vector length remains set to 3, because the length of a vector is equal to the position of the last defined element. The element at position 2 is now undefined, as indicated by the consecutive commas between elements 1 ("a") and 3 ("c").
kill $vector(v,2)
zwrite v
v={"type":"string", "count":2, "length":3, "vector":["a",,"c"]} ; <VECTOR> |
Add a new element at position 10. The vector count increases to 3 and the vector length increases to 10. The elements at positions 4 through 9 are undefined.
set $vector(v,10,"string") = "d"
zwrite v
v={"type":"string", "count":3, "length":10, "vector":["a",,"c",,,,,,,"d"]} ; <VECTOR> |
Update the value at position 10. The vector length and count remain the same, but the $vector function updates the element value.
set $vector(v,10,"string") = "j"
zwrite v
v={"type":"string", "count":3, "length":10, "vector":["a",,"c",,,,,,,"j"]} ; <VECTOR> |
Update the vector in a loop to populate the missing elements. Use the $char function to convert the ASCII codes of lowercase letters to their string representations. The vector now has its first ten elements defined.
set asciiOffset = 96 // lowercase letters start with ASCII code 97
for i = 1:1:10 set $vector(v,i,"string") = $char(i + asciiOffset)
zwrite v
v={"type":"string", "count":10, "length":10, "vector":["a","b","c","d","e","f","g","h","i","j"]} ; <VECTOR> |
Get Data from Vector
Create a ten-element vector containing random integers from 1 to 100. Display the contents by using the zwrite command. Your output will vary.
for i = 1:1:10 set $vector(v1,i,"integer") = $random(100)+1
zwrite v1
v1={"type":"integer", "count":10, "length":10, "vector":[89,40,20,99,32,61,55,34,19,47]} ; <VECTOR> |
Set various elements of the vector to a variable. The returned values are of the same type as the vector (in this case, integer). The exception is the out-of-bounds element, which is set to the empty string.
set element1 = $vector(v1,1)
set element2 = $vector(v1,2)
set element100 = $vector(v1,100)
zwrite element1, element2, element100
element1=89
element2=40
element100="" |
Get the first five elements of the vector. To obtain a range of consecutive elements from a vector, you return a new vector containing only those values. This vector is sometime called a vector slice.
set v2 = $vector(v1,1,5)
zwrite v2
v2={"type":"integer", "count":5, "length":5, "vector":[89,40,20,99,32]} ; <VECTOR> |
Delete the first element from the original vector and get the first five elements again. The returned vector slice contains five elements, but the first element is undefined.
kill $vector(v1,1)
set v2 = $vector(v1,1,5)
zwrite v2
v2={"type":"integer", "count":4, "length":5, "vector":[,40,20,99,32]} ; <VECTOR> |
Delete the fifth element from the original vector and get the first five elements again. The returned vector slice does not include undefined element 5, because this element comes at the end of the vector.
kill $vector(v1,5)
set v2 = $vector(v1,1,5)
zwrite v2
v2={"type":"integer", "count":3, "length":4, "vector":[,40,20,99]} ; <VECTOR> |
Get the elements from element 6 up to the second-to-last element of the original vector. Store these elements in a new vector. Specify the second-to-last element as relative to *, which refers to the index position of the last vector element.
set v3 = $vector(v1,6,*-1)
v3={"type":"integer", "count":4, "length":4, "vector":[61,55,34,19]} ; <VECTOR> |
Get the last element of the vector and specify an out-of-range end position. The $vector function returns a one-element vector containing only the last element of the original vector.
set v4 = $vector(v1,*,100)
v4={"type":"integer", "count":1, "length":1, "vector":[47]} ; <VECTOR> |
Store and Display Timestamp Vectors
Vectors encode timestamps in an integer-based format called Posix time (sometimes called Unix time or Epoch time), where each integer is the number of seconds since (or before) January 1, 1970 00:00:00. For more details on this format, see %Library.PosixTimeOpens in a new tab.
Capture the current time in one-second increments for five seconds. Use the $datetime function to convert the times to the Posix format, which has a conversion code of –2 for this function. Store these times in a vector and display the vector contents. Your times will vary.
set posix = -2
for i=1:1:5 set $vector(v,i,"timestamp") = $zdatetime($now(),posix) hang 1
zwrite v
v={"type":"timestamp", "count":5, "length":5, "vector":[1639672461,1639672463,1639672464,1639672465,1639672466]} ; <VECTOR> |
Display the times stored in the vector in a readable format.
for i=1:1:5 set ts=$vector(v,i) write !,$zdatetime($zdatetimeh(ts,posix))
12/16/2021 11:34:21
12/16/2021 11:34:23
12/16/2021 11:34:24
12/16/2021 11:34:25
12/16/2021 11:34:26 |
You can also convert between timestamp displays by using the LogicalToDisplay and DisplayToLogical methods of %Library.PosixTimeOpens in a new tab. For example, this routine converts raw timestamp dates to the Posix time format, stores the timestamp dates in a vector, and then displays that time in a readable format.
set dates = "10/10/2010 10:10:10;11/11/2011 11:11:11;12/12/2012 12:12:12"
set delimiter = ";"
set numDates = $length(dates,delimiter)
for i = 1:1:numDates
{
set dateString = $piece(dates,delimiter,i)
set datePosix = ##class(%Library.PosixTime).DisplayToLogical(dateString)
set $vector(v,i,"timestamp") = datePosix
write !,##class(%Library.PosixTime).LogicalToDisplay($vector(v,i))
}