My first question here on the board was regarding adding and removing problems based on what was found in the problem list. I got some great help, and the full solution involved using getField() twice and running a for loop over a subarray.
For the life of me, I can't make it work again... I know I'm missing something ridiculously small and obvious, I just can't figure it out.
I need to check the problem list for cirrhosis and alter a variable based on a patient diagnosis. I am using the ICD10 code to determine the diagnosis. For whatever reason, my for loop isn't actually looping. I am only able to check the ICD10 code on the last problem in the list, no matter what the problem is. If I remove a problem, then the loop matches whatever the new last problem is.
Here's what I have:
{
{
fn hasCirrhosis() {
local ProbArray = ""
local ProbSubArray = ""
local hasCirrhosis = ""
ProbArray = getfield(PROB_AFTER("delimited"),"|","")
i=0
for i = 1, i 0 then
hasCirrhosis = "Yep"
else
hasCirrhosis = "Nope"
endif
endfor
if (hasCirrhosis == "Yep") then
userok("YEP")
else
userok("NOPE")
endif
DOCUMENT.PROB_DATA = ProbArray
DOCUMENT.DATA = ProbSubArray
}
}
The two DOCUMENT variables are data displays I am using to look at the two arrays. ProbSubArray is only displaying the last Problem on the list. ProbArray displays all the problems. The function is tied to a button for testing.
What am I missing? This is driving me insane...
I think that something happened to your code when you copy/pasted it into CHUG, because just looking at this, I am surprised it even runs. unless it is psuedo code, in which case, I'll shut up.
could you email me the code you are using, (or attempt to re-paste into this post) and I can give it my best shot. I do for loops and parse out arrays just like this all the time, and it is easy to miss a little thing here or there. here is how I would do the for loop section of your code:
ProbArray = getfield(PROB_AFTER("delimited"),"|","")
hasCirrhosis = "Nope"
local ProbSubArray = ""
for i = 1, i<= size(ProbArray), i = i=+1 do
ProbSubArray = getfield(ProbArray[i],"^","")
if ProbSubArray[i][9] == "ICD10-XX.XXX" then
hasCirrhosis = "Yep"
endif
endfor
UserOK(hasCirrhosis)
//end code
The main difference in logic is that you should set hasCirrhosis = "Nope" at the beginning. if you set it to change to "Yep" and "Nope" in the for loop, you will always only get the comparison from the last problem in the array. Hope this can help you figure stuff out.
Thank you,
Daniel Carpenter
One other addition to Daniel's code to make it leaner - once the condition has been met, no need to keep looping through the balance of the array - insert a 'break' statement and move to the end of the loop immediately. This could eliminate the need to set the variable to 'nope', unless the code explicitly required it.
Taking the hasCirrhosis variable out completely did the job... Then the loop went through every problem, so I broke it out, like Lee recommended.
What I don't understand is why having a variable set to change inside the for loop makes it focus on the last problem in the list. Is it because the code is saying to run through the entire list THEN evaluate the variable?
I feel like I've learned a lot about MEL in the past six months, but then I hit these bumps and I feel like I know nothing at all...
I figured something else out that was causing my problem... It wasn't that I was using a variable, it was that I had a different value for that same variable in my "else" statement.
So say I have 4 problems. I'm looking for Problem 3.
If Problem == "condition" then
variable = "HOORAY"
else
variable = "BOOO"
endif
Walking through the process on paper helped me out here:
Problem 1 "condition"
var = "BOOO"
Problem 2 "condition"
var = "BOOOO"
Problem 3 == "condition"
var = "HOORAY"
I didn't break out of the loop here, so...
Problem 4 "condition"
var = "BOOO"
My end result is var == "BOOO" which makes my visual check evaluate incorrectly.
I can either remove the variable in the "else" statement or I can put a break in after a successful match.
Regardless, I have learned something new today! Thanks again for the help and the guidance.
The issue is that the variable gets reassigned every iteration of the loop, meaning that only the last iteration is the value that is processed after the loop has completed. This makes using the break statement very useful.
Similarly, the 'continue' statement offers some advantages inside functions. It basically says 'if true, do not execute code past this statement in the loop and move to the next iteration'. It is a great utility for parsing a loop with 'some match while others do not' allowing you to code only for positive matches to defined standards and 'auto-skipping' those that do not meet them. 🙂