Previous Thread
Next Thread
Print Thread
foreach .NEXT() Question #37383 10 Jun 24 10:51 PM
Joined: Aug 2016
Posts: 371
J
John Andreasen Offline OP
Member
OP Offline
Member
J
Joined: Aug 2016
Posts: 371
Hi,

I was looking for a way to look ahead in a foreach loop and found .NEXT() mentioned in the docs. At first glance, it looked like what I was needing. In testing though, it appears to be returning 1 or 0 based on whether there is another element to come or not (instead of the key as the docs state.)
Code
FOREACH $$i in $countyUnitTotals()
	Trace.Print "A:"+.KEY($$i)
	Trace.Print "B:"+.NEXT($$i)
NEXT $$i

Quote

A:BAXTER TON 2000.0
B:1
A:CARROLL TON 2000.0
B:1
A:SEARCY TON 2000.0
B:1
A:}}UNKNOWN TON 2000.0
B:0


Is there a way to get the next key during iteration?

Thanks,
John Andreasen
Diversified Data Software

Re: foreach .NEXT() Question [Re: John Andreasen] #37384 10 Jun 24 11:57 PM
Joined: Jun 2001
Posts: 11,794
J
Jack McGregor Offline
Member
Offline
Member
J
Joined: Jun 2001
Posts: 11,794
The documentation is correct except that .NEXT() returns a number 0 / 1 rather than a string. But as it suggests, the function is mainly used internally to implement the next <iterator> statement. Unlike a normal for/next loop, there's no simplistic way to examine the next value without actually advancing to it.

However, you could create a do-it-yourself version of the foreach...next loop as follows:
Code
dimx $sequence, ordmap(varstr;varstr)
map1 i,i,2

$sequence("aaa") = "one"
$sequence("bbb") = "two"
$sequence("ccc") = "three"
$sequence("xxx") = "done"

dimx $$i, iterator($sequence(), "", "", 0)  ! stkey="", endkey="", fwd
do
    i += 1
    ? "Loop iter #";i; .key($$i);" -> ";$$i
    if .next($$i) then
        ? "  next: ";.key($$i);" -> ";$$i
    else
        ? "<end>"
        exit
    endif
loop

Code
.run iternext
Loop iter # 1 aaa -> one
  next: bbb -> two
Loop iter # 2 bbb -> two
  next: ccc -> three
Loop iter # 3 ccc -> three
  next: xxx -> done
Loop iter # 4 xxx -> done
<end>

Otherwise, it would be relatively simple (I think) to implement a .PREV($$i) function, in which case your loop would look something like:
Code
FOREACH $$i in $countyUnitTotals()
	Trace.Print "A:"+.KEY($$i)
         if .NEXT($$i) then
	    Trace.Print "B:"+.KEY($$i)
            if .PREV($$i) = 0 then 
               ? "<end>"
               exit
            endif
         endif
NEXT $$i

I suppose we could take that one step further and implement a .NEXTKEY($$i) function that combined the .NEXT($$i), .KEY($$i) and .PREV($$i) functions, but it would probably need to be logically paired with a .NEXTVALUE($$i) corresponding to $$i to be complete. Is it worth it?

Re: foreach .NEXT() Question [Re: John Andreasen] #37385 11 Jun 24 12:18 AM
Joined: Aug 2016
Posts: 371
J
John Andreasen Offline OP
Member
OP Offline
Member
J
Joined: Aug 2016
Posts: 371
Hi Jack,

Thanks for the quick response and the detailed suggestions. I am not sure whether or not it is worth adding anything or not. I was trying to do something cleaner with breaking on the first part of the key changing. It is perfectly doable another way, but wanted to ask to see if there was an alternative.

Take care,
John

Re: foreach .NEXT() Question [Re: John Andreasen] #37386 11 Jun 24 12:58 AM
Joined: Jun 2001
Posts: 11,794
J
Jack McGregor Offline
Member
Offline
Member
J
Joined: Jun 2001
Posts: 11,794
John -
Well it was an interesting question in any case! Let me know if you see a useful purpose for .PREV($$i) or .NEXTKEY($$i) / .NEXTVALUE($$i)
Regards,
Jack

Re: foreach .NEXT() Question [Re: John Andreasen] #37387 11 Jun 24 10:44 PM
Joined: Aug 2016
Posts: 371
J
John Andreasen Offline OP
Member
OP Offline
Member
J
Joined: Aug 2016
Posts: 371
OK, will do. Thank you Jack.


Moderated by  Jack McGregor, Ty Griffin 

Powered by UBB.threads™ PHP Forum Software 7.7.3