On Oct 3, 3:12 am, John Murray <jo...@[EMAIL PROTECTED]
> wrote:
> S=E9bastien de Mapias asked:
>
> > I saw Mumps examples like:
> > ~ set ^Stuff(1) =3D "ABC`12380`xyz"
> > ~ set ^Stuff(2) =3D "DEF`98787`ghf"
> > etc.
> > and then the following
> > ~ set data=3D$get(^Stuff(2))
> > ~ set field1=3D$piece(data,"`",1)
> > ~ set field2=3D$piece(data,"`",2)
> > ~ etc.
>
> > but what would be the way to retrieve Stuff where
> > field2 =3D 12380 for instance ? what's the syntax ?:
> > ~ set stuff =3D [get ^Stuff where 2nd field =3D 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=3D""
> for set i=3D$order(^Stuff(i)) quit:i=3D"" if
> $piece($get(^Stuff(i)),"`",2)=3D12380 do ProcessStuff(i) quit
> quit
> ProcessStuff(i) ;
> new data
> set data=3D^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)=3Di (if there can only be a
maximum
> of one record with a given piece-2 value), or ^StuffX(piece2,i)=3D"" (if
> there can be multiple records with a given piece-2 value).
>
> Then (first case):
>
> set i=3D$get(^StuffX(12380))
> if i'=3D"" do ProcessStuff
>
> Or (second case):
>
> set i=3D""
> for set i=3D$order(^StuffX(12380,i)) quit:i=3D"" 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)=3D""
> ^StuffX(3,piece3,i)=3D""
>
> 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=3D"" ;this is a random counter variable. It could be anything.
set value=3D12380 ;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=3D$order(^Stuff(i)) quit:i=3D"" if $piece($get(^Stuff(i)),"`",
2)=3Dvalue 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=3D$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=3D"" - 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=3D"". Then this quit
condition will be true and the looping will stop.
if $piece($get(^Stuff(i)),"`",2)=3Dvalue -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)=3Dvalue -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=3Dvalue -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


|