Question
· Feb 24, 2022

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

 

ClassMethod Iterate()
{
set subscript = ""
for {
set subscript = $order(^Book(subscript))
quit:(subscript = "")
write !, "subscript=", subscript, ", value=", ^Book(subscript)
}
}

Thanks,

Prashanth

Product version: IRIS 2022.1
Discussion (4)1
Log in or sign up to continue

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.

 ZSUBFINDER ; 
 ;
    set subscript = ""
    for {
      set subscript = $order(^Book(subscript))
      quit:(subscript = "")
      set moresub = $data(^Book(subscript))
      if moresub=10 {
      set sub2=""
      for {
           set sub2 = $order(^Book(subscript,sub2))
           quit:(sub2="")
           set moresub2= $data(^Book(subscript,sub2))
           if moresub2=10 {
          set sub3=""
             for {
                 set sub3 = $order(^Book(subscript,sub2,sub3))
                 quit:(sub3="")
                 set moresub3= $data(^Book(subscript,sub2,sub3))
                 if moresub3 = 1 {
                 write !, "subscript=", subscript, ", sub2=", sub2, ", sub3=", sub3, ", value=", ^Book(subscript,sub2,sub3)
                 }
             }
           else {
             if moresub2=1 {
                     write !, "subscript=", subscript, ", sub2=", sub2, ", value=", ^Book(subscript,sub2)
             }
           }
      }
      else {
      if moresub=1 {
      write !, "subscript=", subscript, ", value=", ^Book(subscript)
      }
      }
    }
 quit

Hope this helps!