Skip to content

Commit 3bcbb36

Browse files
authored
Can now differentiate between dynamic objects and arrays. Also removed unnecessary code.
1 parent 06ef41d commit 3bcbb36

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

Foreach.cls

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@ Class User.Foreach
55
// Example of how to use the foreach functionality
66
ClassMethod SampleMethod() as %Status {
77
try {
8-
//set message = {}
8+
// Troubleshooting vars for dynamic objects and arrays, activate as needed
9+
set message = {}
10+
set message.sidjfdf = "Dynamic 1"
11+
set message.sdjfs = ""
12+
set message.sdiofsoi = "Dynamic 2"
13+
/*
914
set message(1) = "thing 1"
1015
set message(2) = "thing 2"
16+
*/
17+
// Pass the object, the function we want run, and our class location to the foreach
1118
do ..Foreach(.message, "Action", $this)
1219
}
1320
catch(e) {
@@ -31,14 +38,13 @@ ClassMethod Foreach(ByRef pVal, pFunc, pClass) As %Status {
3138
try {
3239
// We get the key value pairs from our dynamic object
3340
set tIter = pVal.%GetIterator()
41+
if tIter = "" throw
3442
// Loop through these values and add to our query depending on their contents
3543
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-
}
44+
// We pass the value to the action
45+
// TODO: Add ability to capture value and key in a friendly way
46+
do $classmethod(pClass, pFunc, value)
47+
//write key_"='"_value_"'",!
4248
}
4349
}
4450
catch (e) {
@@ -59,23 +65,4 @@ ClassMethod Foreach(ByRef pVal, pFunc, pClass) As %Status {
5965
}
6066
return $$$OK
6167
}
62-
63-
// Iteration over an object
64-
ClassMethod Foreachs(ByRef pVal, pFunc, pClass) As %Status {
65-
try {
66-
// Element position init
67-
set tStruct=""
68-
// Loop through the array
69-
for {
70-
set tStruct=$order(pVal(tStruct))
71-
quit:tStruct=""
72-
// Perform our action method on each element
73-
do $classmethod(pClass, pFunc, pVal(tStruct))
74-
}
75-
}
76-
catch (e) {
77-
write "Foreach error " _e.Name
78-
}
79-
return $$$OK
80-
}
8168
}

0 commit comments

Comments
 (0)