@@ -5,9 +5,10 @@ Class User.Foreach
55// Example of how to use the foreach functionality 
66ClassMethod  SampleMethod () as  %Status  {
77try  {
8- set  message (1 ) = " Hey there" 
9- set  message (2 ) = " Hey thereeee" 
10- do  ..Foreach ($this , " Action" message )
8+ //set message = {} 
9+ set  message (1 ) = " thing 1" 
10+ set  message (2 ) = " thing 2" 
11+ do  ..Foreach (.message , " Action" $this )
1112}
1213catch (e ) {
1314write  " SampleMethod error" e .Name 
@@ -26,8 +27,41 @@ ClassMethod Action(pMes) as %Status {
2627return  $$$OK
2728}
2829
30+ ClassMethod  Foreach (ByRef  pVal , pFunc , pClass ) As  %Status  {
31+ try  {
32+ // We get the key value pairs from our dynamic object 
33+ set  tIter  = pVal .%GetIterator ()
34+ // Loop through these values and add to our query depending on their contents 
35+ while  tIter .%GetNext (.key , .value ) {
36+ if  value  '= " " 
37+ // We pass the value to the action 
38+ // TODO: Add ability to capture value and key in a friendly way 
39+ do  $classmethod (pClass , pFunc , value )
40+ //write key_"='"_value_"'",! 
41+ }
42+ }
43+ }
44+ catch  (e ) {
45+ try  {
46+ // Element position init 
47+  set  tStruct =" " 
48+  // Loop through the array 
49+  for  {
50+  set  tStruct =$order (pVal (tStruct ))
51+  quit :tStruct =" " 
52+  // Perform our action method on each element 
53+  do  $classmethod (pClass , pFunc , pVal (tStruct ))
54+ }
55+ }
56+ catch  (e ) {
57+ write  " Foreach error " e .Name 
58+ }
59+ }
60+ return  $$$OK
61+ }
62+ 
2963// Iteration over an object 
30- ClassMethod  Foreach ( pClass , pFunc , ByRef   pVal ) As  %Status  {
64+ ClassMethod  Foreachs ( ByRef   pVal , pFunc , pClass ) As  %Status  {
3165try  {
3266 // Element position init 
3367 set  tStruct =" " 
@@ -36,7 +70,7 @@ ClassMethod Foreach(pClass, pFunc, ByRef pVal) As %Status {
3670 set  tStruct =$order (pVal (tStruct ))
3771 quit :tStruct =" " 
3872 // Perform our action method on each element 
39-  do  $CLASSMETHOD  (pClass , pFunc , pVal (tStruct ))
73+  do  $classmethod  (pClass , pFunc , pVal (tStruct ))
4074}
4175}
4276catch  (e ) {
0 commit comments