Search code examples
intersystems-cachemumps

Searching a Global for A string


I'm trying to figure out the best way to traverse a global below is what I have written.

s X="^ZNAME"
r !,"Please insert a name or a portion of a name: ",str
d {
      s X=$Q(@X) Q:X=""
      i X[str!(@X[str) 
      {
          w !,X
      }
  } While X'=""
q

I receive the following result ^ZName(subscript) if the name or portion of the name matches what is in the global. What do you guys suggest would be the best way to extract just the subscript? I was thinking $E or $P, but I don't think it would be specific enough. Also, if there are any additional books or websites that do a great job of instructing M, I would love to know and be very grateful. Thank you for all of your help.


Solution

  • In first you should look at official documentation, it is available in menu by cube in tray. And then you should look at $QUERY, which you use here, and $ORDER. So, there are big difference between both of this functions. $Query used to full scan for global, while $Order only for one level. If you still want to scan by $Query, you may look at $QSubscript function, which may help you to get value for particular subscript.
    with $order, it may looks so

    set index=""
    for { 
      set index=$order(@X@(index))
      quit:index=""
    
      write !,index
    
      // and for next level
      set index2=""
      for {
        set index2=$order(@X@(index, index2))
        quit:index2
    
        write !?5,index2
      }
    }
    

    Also, you may find something interesting in a new Developer Community portal. And get some online courses by InterSystems here.