How to Iterate un even Multi Dimensional Array global Programmatically
Hi Community,
I have created global with below values , now I want to iterate programatically.
^Book("Auto1","Properties","Color")="Red"
^Book("Auto1","Properties","Color1")="Yellow"
^Book("Auto1","Properties","Model")="SUV"
^Book("Auto2","Owner")="Prashanth"
^Book("Auto2","Properties","Color")="Green"
^Book("Color")="Red"
^Book("Color1")="Red"
i have used below method but only printing last2 elements properly
{
set subscript = ""
for {
set subscript = $order(^Book(subscript))
quit:(subscript = "")
write !, "subscript=", subscript, ", value=", ^Book(subscript)
}
}
Thanks,
Prashanth
$ORDER() always operates at the distinct subscript level you start it Auto1,Auto2, Color,Color2
to check for a deeper level you may use $DATA()
or you run your search using $QUERY() which returns only instances with data
Hi.
You should use function $QUERY ($QUERY | ObjectScript Reference | InterSystems IRIS Data Platform 2020.1
).
Regards,
Matjaž
Well, everyone has coding styles and ObjectScript offers several different styles - I could have made this prettier (to me, anyway) as I'm more accustomed to the single-letter-command and dot-loop styles... but I tried to keep this in your coding style.
My code isn't pretty - I focused more on making it (barely) functional and demonstrative of the $DATA command - this command will let you know if there's any further subscripts available in a global - documentation page is here:
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_FDATA
Anyway, here's my code - I didn't have a chance to create a class method (again, I prefer the older styles) but just copy-paste the code center into your method and it should function. Again, it's not pretty, but it will demonstrate what you need.
If you wanted to make this more efficient, recoding this to handle subscripts recursively would be much shorter and could handle any number of subscripts, not just 3.
Hope this helps!
Thanks a lot, Roger, yes working fine.
thanks for your time and effort to share the code snippet.