0

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.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
M Noob
  • 19
  • 4

3 Answers3

2

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.

tsafin
  • 151
  • 6
DAiMor
  • 3,185
  • 16
  • 24
  • Thank you so much guys!! Never knew that programming could be so much fun. I can't wait to become more advanced. – M Noob Mar 22 '16 at 19:48
2

There is a Find query in a %Global class. The query returns the nodes that contain FindWhat, one node per row, with four columns, namely Name, Value, Name Format and Value Format. Use ##Class(%Library.Utility).Replace to replace FindWhat with ReplaceWith.

NameFormat:

  1. String format, this is the value of the subscript unaltered
  2. Cache format, the value has been converted into the Cache representation for easy viewing, for example instead of showing the binary value a list is displayed as '$lb(1,"test")'. This format is suitable as the argument of $name.

ValueFormat:

  1. String format, this is the value unaltered
  2. Cache format, the value has been converted into the Cache representation for easy viewing, for example instead of showing the binary value a list is displayed as '$lb(1,"test")'.

On output, Name Format and Value Format may assume a third value, namely 3, which means "Not editable". Due to issues with the length of this data not all of it can be displayed so a portion is displayed.

rfg
  • 1,331
  • 1
  • 8
  • 24
0
SEARCH(where,what)
        ; Search string in a Global or Array
        I $G(where)="" Q
        I $G(what)="" Q
        F  S where=$Q(@where) Q:where=""  D
        .       I ((where[what)!(@where[what)) D
        ..              W where,"=",@where,!
        ..              W $E(where,$F(where,"("),$L(where)-1),!
        Q

To extract the subscript (key or keys, since multiple can exist) I would use $E and $F since would give problems when "(" or ")" are part of the subscript.

W $E(where,$F(where,"("),$L(where)-1)

Tested on GTM.

Normal input:

GTM>ZWR ^ZNAME
^ZNAME("first,last")="Second Street"
^ZNAME("name,surname")="First Street"

GTM>D SEARCH^ZZTEST("^ZNAME","last")
^ZNAME("first,last")=Second Street
"first,last"

GTM>D SEARCH^ZZTEST("^ZNAME","Street")
^ZNAME("first,last")=Second Street
"first,last"
^ZNAME("name,surname")=First Street
"name,surname"

GTM>D SEARCH^ZZTEST("^ZNAME(""first,last"")","Street")
^ZNAME("name,surname")=First Street
"name,surname"

GTM>D SEARCH^ZZTEST("^ZNAME",",sur")
^ZNAME("name,surname")=First Street
"name,surname"

As a bonus you can also use it to search local arrays:

GTM>ZWR ARRAY
ARRAY(1)="Apple"
ARRAY(1,1)="Apple pie"
ARRAY(2)="Orange"

GTM>D SEARCH^ZZTEST("ARRAY","Apple")
ARRAY(1)=Apple
1
ARRAY(1,1)=Apple pie
1,1

GTM>D SEARCH^ZZTEST("ARRAY","pie")
ARRAY(1,1)=Apple pie
1,1

And no problem with invalid inputs:

GTM>K ARRAY

GTM>D SEARCH^ZZTEST("ARRAY","Apple")

GTM>D SEARCH^ZZTEST("ARRAY","")

GTM>D SEARCH^ZZTEST("ARRAY",)

GTM>D SEARCH^ZZTEST("ARRAY")

GTM>D SEARCH^ZZTEST()

GTM>D SEARCH^ZZTEST("^ZDOESNOTEXIST")

Handling "(" and ")" in the subscript:

GTM>ZWR ARRAY
ARRAY("()")=1
ARRAY("1(")=1
ARRAY("1()")="Orange"
ARRAY("1)")="Apple"

GTM>D SEARCH^ZZTEST("ARRAY","()")
ARRAY("()")=1
"()"
ARRAY("1()")=Orange
"1()"

GTM>D SEARCH^ZZTEST("ARRAY","Apple")
ARRAY("1)")=Apple
"1)"
C4xuxo
  • 18
  • 4