intersystems-cachemumps

Searching for a Name in a Global using a Partial Name


I created a name global and I am trying to print out a matching name by using only using the same characters that the name starts with. Example: Enter Sm and return the value Smith, John A.

I created this:

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

I entered two names and got the desired result.^ZNAME("MITCHELL, DAVID J")^ZNAME ("SMITH, JOHN A").

I want to to be able to read a partial name and it search the ^ZNAME and return the value it matches. In this case read "Sm" and return "Smith, John A."

N partial,val
S partial="Enter a name or partial name: "
F  W !,partial R val Q:val=""  D
.   W !,$O(^ZNAME("val"))
Q

When I enter "Sm" from the read command it loops back to Enter a name or partial name instead of giving me the desired result of Smith, John A. I am missing something I know it, but a little burnt out. Any help will be great thank you!


Solution

  • You've got double quotes around val:

    .   W !,$O(^ZNAME("val"))
    Q
    

    So it is trying to write the value at ^ZNAME("val") which there isn't one. Remove the double quotes and it should work.