0

I am working to create a program in M that reads an input of names in a certain format. Once the user places a null value it prints out all of the values. Where I am getting stuck is reading into a global variable. I look forward to any input I can receive.

          n prompt,val,done
          s prompt="Enter a name (LAST,FIRST MI): "
          s val="" f in=1:1 s val=$O(^ZNAME(val)) q:val=""
          f  w !,prompt r val q:val=""  d   q:done 
          . i val'?1.A1",".1" "1.A.1(1" "1A) w !,"Invalid name" q
          . s val=$GET(^ZNAME)
          . s done=1
          i val="" q  
          w !,"You entered: ",val
         . s done=1
         q
M Noob
  • 19
  • 4

2 Answers2

1

Your code looks strange. In first this line s val="" f in=1:1 s val=$O(^ZNAME(val)) q:val="" is useless, just order all values in first subscript of global. Then your code should fail with UNDEFINED error for done variable when entered val will passed your pattern. And finally it gets new value from global ^ZNAME, and does not matter what was entered, it will get one value for all times.

DAiMor
  • 3,185
  • 16
  • 24
0

I don't completely understand what you want to do but this is what I built:

  • read names till you give an empty name
  • if the name follows a pattern add him to ^ZNAME
  • at the end print all names in ^ZNAME

Here you go:

readlist
    N prompt,val
    S prompt="Enter a name (LAST,FIRST MI): "
    F  W !,prompt R val Q:val=""  D
    .   I val'?1.A1",".1" "1.A.1(1" "1A) W !,"Invalid name"
    .   E  S ^ZNAME(val)=""
    F  S val=$O(^ZNAME(val)) Q:val=""  D
    .   W !,"You entered: ",val
    Q

Example (using GTM):

GTM>D readlist^ZZTEST

Enter a name (LAST,FIRST MI): first,last
Enter a name (LAST,FIRST MI): name,surname
Enter a name (LAST,FIRST MI):
You entered: first,last
You entered: name,surname
GTM>zwr ^ZNAME
^ZNAME("first,last")=""
^ZNAME("name,surname")=""
C4xuxo
  • 18
  • 4
  • I basically bought a book on M programming and as I go through a chapter I try to combine everything I've learned and make practice problems for myself. Thank you DAimor and user3532194. A few examples I saw on the internet actually had the $Order as one of the first lines of code. – M Noob Mar 18 '16 at 13:32