A $list is usually created using the $LISTBUILD() function, and accessed with the $LIST() function:
USER:;L=$LISTBUILD("A","B",1,"2")
USER:;CRT $LIST(L,2)
B
A $list is stored as a string, so it could be displayed and manipulated as a string, but in general should not be because it contains binary characters. A $list should not be stored in a dynamic array or in an mv file because it could contain system delimiters.
If you are curious what is actually inside of a $list:
USER:;CRT OCONV(L,'MCP')
..A..B.....2
USER:;CRT OCONV(L,'MCAX')
030141030142030401030132
The MCP conversion displays non-printable characters as dots, and the MCAX conversion displays the ascii values of characters as 2 hex digits. Displaying the $list unconverted with a CRT statement could have bad results if it contains binary characters that a terminal recognizes as control strings. However, the Object Script ZWRITE command knows how to display a $list:
USER:; ^list=L
USER:; $xecute("zwrite ^list")
^list=$lb("A","B",1,"2")
And you can also display the value of $lists in objects:
USER:;s="%ListOfDataTypes"->%New()
USER:;s->Insert($LISTBUILD("ABC",1,"2"))
USER:;$SYSTEM.OBJ->Dump( s)
+----------------- general information ---------------
| oref value: 3
| class name: %Library.ListOfDataTypes reference count: 0
+----------------- attribute values ------------------
| Data(1) = $lb("ABC",1,"2")
| ElementType = ""
| Size = 1 <Set>
However, the actual format of a $list is not documented, and you shouldn't go inside. Usually, a $list is only used with a set of functions that know how to handle a $list. These functions are intrinsic in both Object Script and Mv Basic:
$LIST --retrieve elements from a list
$LISTBUILD -- create a list
$LISTFROMSTRING --create a list from a delimited string
$LISTTOSTRING -- create a delimited string from a list
$LISTFIND -- find a string in a list
$LISTNEXT -- traverse a list in order
$LISTLENGTH -- get the number of elements in a list
$LISTSAME -- compare lists
$LISTVALID -- determine if a string contains a list
$LISTDATA -- determine if a list contains data in an element
$LISTGET -- retrieve elements from a list that may contain empty elements
A $list can be efficiently traversed using $LISTNEXT():
L=$LISTBUILD("A","B","C","D")
CNT=0
LOOP WHILE $LISTNEXT(L,CNT,ELEMENT) # 0 DO
CRT ELEMENT
REPEAT
Which will print out all the elements of the list. $$LISTNEXT() is much faster than using $LISTLENGTH() and a for/next loop.
A $list can contain other $lists as elements, which in turn can contain other $lists. A $list can also contain null elements, which are different from empty elements (similar to the SQL NULL concept):
USER:; L1=$LISTBUILD("A","","B")
USER:; L2=$LISTBUILD("A",,"B")
USER:;CRT $LIST(L1,2)
USER:;CRT $LIST(L2,2)
<NULL VALUE>
In L1, the second element is an empty string, but in L2 the second element is null. Accessing a null element with $LIST() will throw a <NULL VALUE> exception. You can use $LISTGET() to handle possibly null elements:
USER:;CRT $LISTGET(L2,2,"i am null")
i am null
And you can use $LISTDATA() to determine if an element is null:
USER:;CRT $LISTDATA(L2,2)
0
USER:;CRT $LISTDATA(L1,2)
1
If a list contains only 3 elements, and you ask $LIST() to return element #4, you also get a <NULL VALUE> exception.
$LISTVALID(s) returns true if string s is a $list:
USER:;crt $LISTVALID("abc")
0
An empty string "" *is* a valid list, containing no elements.
$lists can be concatenated with each other forming a larger list:
(using L, L1, and L2 defined in previous examples) USER:; L3=L:L1:L2 USER:; CRT $LISTLENGTH(L3) 10
$lists remember the data type of elements they are built with:
USER:;^L=$LISTBUILD("AB4C","1",2,FDIV(9,3))
USER:[ZW ^L
^L=$lb("AB4C","1",2,3)
Note that in the zwrite output, "1" is in quotes because it was passed as a string. 2 doesn't have quotes because it was passed as a number. The 4th element 3 also displays as a number, but it is actually stored in the $list as a floating point number.
The object library provides a datatype class %Library.List, or %List to represent $lists.
Caché Object Script provides support for a specially formatted string called a $list. A $list is used in object script in many of the places where a dynamic array would be used in mv basic. As with a dynamic array, elements can be added, updated, or removed at any location in a $list. Unlike a dynamic array, a $list can contain any character--elements in a $list are stored in a binary structure rather than being separated by system delimiters. This structure also makes accessing arbitrary elements faster than in a dynamic array. $Lists are used extensively by Caché Objects and in the object library, so intrinsic support for $lists has been added to the mvbasic language. A $list is usually created using the $LISTBUILD() function, and accessed with the $LIST() function: USER:;L=$LISTBUILD("A","B",1,"2") USER:;CRT $LIST(L,2) B A $list is stored as a string, so it could be displayed and manipulated as a string, but in general should not be because it contains binary characters. A $list should not be stored in a dynamic array or in an mv file because it could contain system delimiters. If you are curious what is actually inside of a $list: USER:;CRT OCONV(L,'MCP') ..A..B.....2 USER:;CRT OCONV(L,'MCAX') 030141030142030401030132 The MCP conversion displays non-printable characters as dots, and the MCAX conversion displays the ascii values of characters as 2 hex digits. Displaying the $list unconverted with a CRT statement could have bad results if it contains binary characters that a terminal recognizes as control strings. However, the Object Script ZWRITE command knows how to display a $list: USER:; ^list=L USER:; $xecute("zwrite ^list") ^list=$lb("A","B",1,"2") And you can also display the value of $lists in objects: USER:;s="%ListOfDataTypes"->%New() USER:;s->Insert($LISTBUILD("ABC",1,"2")) USER:;$SYSTEM.OBJ->Dump( s) +----------------- general information --------------- | oref value: 3 | class name: %Library.ListOfDataTypes reference count: 0 +----------------- attribute values ------------------ | Data(1) = $lb("ABC",1,"2") | ElementType = "" | Size = 1 <Set> However, the actual format of a $list is not documented, and you shouldn't go inside. Usually, a $list is only used with a set of functions that know how to handle a $list. These functions are intrinsic in both Object Script and Mv Basic: $LIST --retrieve elements from a list $LISTBUILD -- create a list $LISTFROMSTRING --create a list from a delimited string $LISTTOSTRING -- create a delimited string from a list $LISTFIND -- find a string in a list $LISTNEXT -- traverse a list in order $LISTLENGTH -- get the number of elements in a list $LISTSAME -- compare lists $LISTVALID -- determine if a string contains a list $LISTDATA -- determine if a list contains data in an element $LISTGET -- retrieve elements from a list that may contain empty elements A $list can be efficiently traversed using $LISTNEXT(): L=$LISTBUILD("A","B","C","D") CNT=0 LOOP WHILE $LISTNEXT(L,CNT,ELEMENT) # 0 DO CRT ELEMENT REPEAT Which will print out all the elements of the list. $$LISTNEXT() is much faster than using $LISTLENGTH() and a for/next loop. A $list can contain other $lists as elements, which in turn can contain other $lists. A $list can also contain null elements, which are different from empty elements (similar to the SQL NULL concept): USER:; L1=$LISTBUILD("A","","B") USER:; L2=$LISTBUILD("A",,"B") USER:;CRT $LIST(L1,2) USER:;CRT $LIST(L2,2) <NULL VALUE> In L1, the second element is an empty string, but in L2 the second element is null. Accessing a null element with $LIST() will throw a <NULL VALUE> exception. You can use $LISTGET() to handle possibly null elements: USER:;CRT $LISTGET(L2,2,"i am null") i am null And you can use $LISTDATA() to determine if an element is null: USER:;CRT $LISTDATA(L2,2) 0 USER:;CRT $LISTDATA(L1,2) 1 If a list contains only 3 elements, and you ask $LIST() to return element #4, you also get a <NULL VALUE> exception. $LISTVALID(s) returns true if string s is a $list: USER:;crt $LISTVALID("abc") 0 An empty string "" *is* a valid list, containing no elements. $lists can be concatenated with each other forming a larger list: (using L, L1, and L2 defined in previous examples) USER:; L3=L:L1:L2 USER:; CRT $LISTLENGTH(L3) 10 $lists remember the data type of elements they are built with: USER:;^L=$LISTBUILD("AB4C","1",2,FDIV(9,3)) USER:[ZW ^L ^L=$lb("AB4C","1",2,3) Note that in the zwrite output, "1" is in quotes because it was passed as a string. 2 doesn't have quotes because it was passed as a number. The 4th element 3 also displays as a number, but it is actually stored in the $list as a floating point number. The object library provides a datatype class %Library.List, or %List to represent $lists. -- You received this message because you are subscribed to the Google Groups "InterSystems: MV Community" group. To post to this group, send email to Cac...@googlegroups.com To unsubscribe from this group, send email to CacheMV-u...@googlegroups.com For more options, visit this group at http://groups.google.com/group/CacheMV?hl=en