Skip to content

Commit 29b2d80

Browse files
authored
Added support for single dimensional arrays.
1 parent a816a12 commit 29b2d80

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

Foreach.cls

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
Class User.Foreach
2-
{
3-
// TODO: Add support for multi-dimensional arrays
2+
{
3+
// TODO: Add support for multi-dimensional arrays
44

5-
ClassMethod thing(pMes) as %Status {
6-
try {
7-
write "It worked!" _ pMes
8-
}
9-
catch(e) {
10-
write "thing error" _e.Name
11-
}
12-
return $$$OK
13-
}
5+
// Example of how to use the foreach functionality
6+
ClassMethod SampleMethod() as %Status {
7+
try {
8+
set message(1) = "Hey there"
9+
set message(2) = "Hey thereeee"
10+
do ..Foreach($this, "Action", .message)
11+
}
12+
catch(e) {
13+
write "SampleMethod error" _e.Name
14+
}
15+
return $$$OK
16+
}
1417

15-
ClassMethod X() as %Status {
16-
try {
17-
set message = "Hey there"
18-
do ..Foreach($this, "thing", message)
19-
}
20-
catch(e) {
21-
write "X error" _e.Name
22-
}
23-
return $$$OK
24-
}
18+
// A method that is called on each iteration
19+
ClassMethod Action(pMes) as %Status {
20+
try {
21+
write pMes, !
22+
}
23+
catch(e) {
24+
write "Action error " _e.Name
25+
}
26+
return $$$OK
27+
}
2528

26-
ClassMethod Foreach(pClass, pFunc, pVal) As %Status {
27-
try {
28-
do $CLASSMETHOD(pClass, pFunc, pVal)
29-
}
30-
catch (e) {
31-
write "Foreach error" _e.Name
32-
}
33-
return $$$OK
34-
}
29+
// Iteration over an object
30+
ClassMethod Foreach(pClass, pFunc, ByRef pVal) As %Status {
31+
try {
32+
// Element position init
33+
set tStruct=""
34+
// Loop through the array
35+
for {
36+
set tStruct=$order(pVal(tStruct))
37+
quit:tStruct=""
38+
// Perform our action method on each element
39+
do $CLASSMETHOD(pClass, pFunc, pVal(tStruct))
40+
}
41+
}
42+
catch (e) {
43+
write "Foreach error " _e.Name
44+
}
45+
return $$$OK
46+
}
3547
}

0 commit comments

Comments
 (0)