Question
· Nov 4, 2021

MVBASIC Undimensioned array question

When using an undimensioned array is there a way to write the array to a file? Something like this

DIM C()
C('TEST') = 2
C('WEST') = 5
C('BEST') = 1
WRITE C on myfile, 1
Product version: Caché 2018.1
Discussion (2)1
Log in or sign up to continue

Not sure what you are really trying to do here. Who is going to read this file.?

However: I see this as two parts

First Method: you create a Template of an Array Name, as indicated. Then create Part 2 The second Method: Create/open a %Binarystream File and copy the Array data into the stream from the Known array (Always using the same Array Name) and then save the file.

Since the content of your now Standard Named Array may have a varying level of nodes, use the $Q command to advance through the unknown array levels and save both the level Nodes and the values into the file.

This means you create the Named Array, populated by any number of levels of data, then pass this to the 2nd method to actually populate the physical file.

This is the old technology, which raises the question, what problem are you actually trying to solve? Is this really the best solution.?

Do you mean write the array to an MV file, as you can do with MATWRITE on a dimensioned array? No, not directly. MATWRITE and WRITE work with dimensioned and dynamic arrays because they have an explicit numeric sequence that maps the array elements to the attributes of a file item.

If your array is one-dimensional as in your example, you could use $ORDER in MVBASIC to traverse the array and write each element as a file item where the array subscript becomes the item id.

If you want to store the array in the database persistently, you can store it as a global with $MERGE:

$MERGE ^SOMEGLOBAL=C

or

$MERGE ^SOMEGLOBAL("thisdata")=C

Take a look at this article which talks about $MERGE, $ORDER, and globals in MV.

If you want to write the array to an OS file, you can use the previous suggestion of traversing with $ORDER and writing to a %Stream.File object. If the array is multidimensional, you can use objectscript $QUERY (MVBASIC doesn't support $QUERY directly).

Another option for writing to an OS file is using the %Library.Global class which has Import() and Export() methods. You can $merge your array to a temporary global, and then write that to a file using Export()