Re: Retrieve an array using non-subscript data
- From: lmv <lverhulst@xxxxxxxxx>
- Date: Thu, 04 Oct 2007 09:06:45 -0000
On Oct 3, 3:12 am, John Murray <jo...@xxxxxxxxxxxxxxx> wrote:
Sébastien de Mapias asked:
I saw Mumps examples like:
~ set ^Stuff(1) = "ABC`12380`xyz"
~ set ^Stuff(2) = "DEF`98787`ghf"
etc.
and then the following
~ set data=$get(^Stuff(2))
~ set field1=$piece(data,"`",1)
~ set field2=$piece(data,"`",2)
~ etc.
but what would be the way to retrieve Stuff where
field2 = 12380 for instance ? what's the syntax ?:
~ set stuff = [get ^Stuff where 2nd field = 12380]
so here 'stuff' would be a reference to the array
"ABC`12380`xyz" (that I could further process)...
You either need to iterate through ^Stuff(i) until you find what you
want, or you need to build and maintain an index.
Iteration example:
new i
set i=""
for set i=$order(^Stuff(i)) quit:i="" if
$piece($get(^Stuff(i)),"`",2)=12380 do ProcessStuff(i) quit
quit
ProcessStuff(i) ;
new data
set data=^Stuff(i)
;
; Your logic here
quit
--
Note that the QUIT after DO ProcessStuff(i) means we only process the
first 12380 we find. If you want to process all occurrences, reemove
that QUIT.
Index example:
Construct and maintain ^StuffX(piece2)=i (if there can only be a maximum
of one record with a given piece-2 value), or ^StuffX(piece2,i)="" (if
there can be multiple records with a given piece-2 value).
Then (first case):
set i=$get(^StuffX(12380))
if i'="" do ProcessStuff
Or (second case):
set i=""
for set i=$order(^StuffX(12380,i)) quit:i="" do
ProcessStuff(i) quit
Again, the final QUIT on this line is optional, depending on whether you
want to process all 12380s or only the "first" (the one with the
earliest-collating 'i').
In practice you might want indices on several pieces. You can use the
^StuffX global for all of them (and the global name is merely a
convention):
^StuffX(2,piece2,i)=""
^StuffX(3,piece3,i)=""
I hope this is useful (caveat: none of the above code has been tested by
me prior to posting)
John Murray
George James Softwarewww.georgejames.com
Serenji - don't DO without it
John Murray posted an excellent example. I'd like to explain it a
little more to (maybe) help explain it to a newcomer.
new i,value ;initialize your variables (always a good idea)
set i="" ;this is a random counter variable. It could be anything.
set value=12380 ;this variable 'value' contains the item you are
seeking.
;this item could be passed in from elsewhere or read
from
;a file or the keyboard.
for set i=$order(^Stuff(i)) quit:i="" if $piece($get(^Stuff(i)),"`",
2)=value do ProcessStuff(i)
The above line is complex to a newcomer. Lets break it down.
for - This command followed by 2 spaces is an infinite loop. It will
cause the commands on the remainder of the line to repeat infinitely.
The way to stop the loop is with a quit command (which you see later
on the line).
set i=$order(^Stuff(i)) - The function $order is the command used to
retrieve the nodes on a global (or array). In this example, ^Stuff(i)
is passed in and $order finds the next subscript in the sequence. So,
on the first loop, the initial value of i is "". This gets passed to
the $order function and the function returns the next node in
sequence, which in your example is 1. On the second loop, i is 1 and
when $order gets called, it will return the next node which is 2. When
there are no more subscripts, a "" is returned.
quit:i="" - quit if i equals "" is how I always read this. This is
how the looping will stop. When the $order command reaches the end of
the ^Stuff global, it will return a "" and set i="". Then this quit
condition will be true and the looping will stop.
if $piece($get(^Stuff(i)),"`",2)=value -Lets break this down a little
more:
$get(^Stuff(i)) -Pass in a global reference and return the data if it
exists or "" if it does not. Let's call this data x.
if $piece(x,"`",2)=value -Using the data returned from the $get, call
the $piece function and using "`" as the delimiter, return the 2
piece. Let's call this data y.
if y=value -Using the data returned from the $piece, examine if it is
equal to the value you are looking for. If false, ignore the rest of
the line and continue looping. If true, continue processing the
commands on the line.
do ProcessStuff(i) - Call a tag (or subroutine) to do more
processing. Pass it the variable i which is the node of the global
where the data was found.
quit
No doubt I've misstated some things, but I was trying to keep the
explanation simple and relevant to the example at hand. I hope this
helps you or some other newcomer out.
Regarding indexes. Yes, as others have stated, MUMPS is a data storage
application and not a database management system. It will store the
data however you tell it. But that's just it, you have to tell it. Or
use some other application that has been layered on top of MUMPS (like
the VA's VistA Fileman, or IDX's DBMS or Epic's Chronicles). If you
are making your own application, you will have to make sure that when
you add/edit/delete a field that has an index, then the index will
also need to be added/edited/deleted. How you implement this, is up to
you.
Leane Verhulst
Verhulst Consulting, Inc.
www.verhulstconsulting.com
.
- References:
- Retrieve an array using non-subscript data
- From: Sébastien de Mapias
- RE: Retrieve an array using non-subscript data
- From: John Murray
- Retrieve an array using non-subscript data
- Prev by Date: Re: Retrieve an array using non-subscript data
- Next by Date: Re: Retrieve an array using non-subscript data
- Previous by thread: Re: Retrieve an array using non-subscript data
- Index(es):
Relevant Pages
|