for each statement

sdjensen

Member
for each statement (solved/explained)

Hi Girls and Guys,

Simple question

Should I expect this feature to work in the future?

Code:
def buffer tmpCust for customer.
def var i as int.
for each tmpCust :
  i = i + 1.  
  if i = 1000 then leave. /* (1)*/
end.
 
message avail tmpCust tmpCust.name i view-as alert-box.

if I remove the line at (1) then tmpcust is not avail after the loop and that is as expected but if I leave the line (1) in, then tmpCust record is avail.

I thought that for each ... was a "closed" block.

Comments?
 

rstanciu

Member
this is normal, at the end of for-each the record buffer is not available.

If you leave the block at the 1000 line, is normal that we can find
available the Customer because the last record buffer position is the 1000 line you find.

I don't known what is the question ... is a normal execution plan.
 

StuartT

Member
If you want to go through all the customers in a for each then still have access to the last one you could do the following:

Code:
def var rrecid as recid.
def buffer tmpCust for customer.
def var i as int.
for each tmpCust :
  i = i + 1.  
 assign rrecid = recid(tmpcust).
end.
 find tmpcust where recid(tmpcust) = rrecid.
message avail tmpCust tmpCust.name i view-as alert-box.
 

TomBascom

Curmudgeon
Yes, that feature will always work.

Think of a FOR EACH this way:

Code:
loop: do while true:

  find next customer no-error.
  if not available( customer ) then leave loop.

  /* body of "FOR EACH" */

end.
 

sdjensen

Member
Thanks for all the replies.

In real life I would use the same code as Stuart, I just found the example in some old code one of my colleagues has written og we had a discussion if the code would work or not.

Tom, your example makes sense and I will put it into my bag of function-and-code-examples-for-progress. :awink:
 
Top