[22 Sep 2008] Possible bug in For...Next loop

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

[22 Sep 2008] Possible bug in For...Next loop

Post by Allanon »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 22 Sep 2008 15:37:46 -0000

Hello Andreas, I think I've found a bug in For...Next loop using recursive functions, the following piece of code render an object and check if exists object's childs, if so renders them calling recursively the RenderObject() function:

Code: Select all

Function scui.RenderObject(id)
    ...
    ; RENDER ALL CHILDS -----------------------------------------------
    If helpers.FieldIsNil(Obj, "childlist") = 0
       Local cnt
       Local childs = helpers.CountEntries(Obj.ChildList)
       DebugPrint("RENDERING CHILDS OF ", Obj.oName)
          For cnt = 1 To childs
             DebugPrint("- Child ", scui.IFO[Obj.ChildList[cnt].Id].oName)
             scui.RenderObject(Obj.ChildList[cnt].Id)
          Next
    EndIf
    ...
EndFunction
The above function does not work, because when returning from a recursive call the For...Next exit even if <cnt> has not reached the value stored in the variable <childs>.

Instead, using a While...Wend loop, works correctly:

Code: Select all

Function scui.RenderObject(id)
    ...
    ; RENDER ALL CHILDS -----------------------------------------------
    If helpers.FieldIsNil(Obj, "childlist") = 0
       Local cnt = 1
       Local childs = helpers.CountEntries(Obj.ChildList)
       DebugPrint("RENDERING CHILDS OF ", Obj.oName)
       While cnt <= childs
       ;For cnt = 1 To childs
          DebugPrint("- Child ", scui.IFO[Obj.ChildList[cnt].Id].oName)
          scui.RenderObject(Obj.ChildList[cnt].Id)
          cnt = cnt + 1
       ;Next
       Wend
    EndIf
    ...
EndFunction
Maybe I'm missing something?

Regards, Fabio
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
User avatar
airsoftsoftwair
Posts: 5914
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[22 Sep 2008] Re: Possible bug in For...Next loop

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 22 Sep 2008 23:46:42 +0200
Maybe I'm missing something?
I currently don't have access to the Hollywood sources, so I can't look at the source code but I have an idea what could be the cause. Can you try if the following version works:

Code: Select all

Function scui.RenderObject(id)
    ...
    ; RENDER ALL CHILDS -----------------------------------------------
    If helpers.FieldIsNil(Obj, "childlist") = 0
;       Local cnt
       Local childs = helpers.CountEntries(Obj.ChildList)
       DebugPrint("RENDERING CHILDS OF ", Obj.oName)
;          For cnt = 1 To childs
           For Local cnt = 1 To childs
             DebugPrint("- Child ", scui.IFO[Obj.ChildList[cnt].Id].oName)
             scui.RenderObject(Obj.ChildList[cnt].Id)
          Next
    EndIf
    ...
EndFunction
I've removed the "Local cnt" statement and moved it inside the for loop. Does this work?
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

[23 Sep 2008] Re: Possible bug in For...Next loop

Post by Allanon »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 23 Sep 2008 11:09:15 -0000

Yes, using your example it works correctly! Thanks! One more question, it's just a trick or can I use the form "For Local <var> ... " freely within my code?

Regards, Fabio
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
User avatar
airsoftsoftwair
Posts: 5914
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[23 Sep 2008] Re: Re: Possible bug in For...Next loop

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Tue, 23 Sep 2008 17:56:36 +0200
Yes, using your example it works correctly! Thanks!
Fine, but of course your version should also work so I think you've really found a VM bug here. I'll investigate the issue this weekend.
One more question, it's just a trick or can I use the form "For Local <var> ... " freely within my code?
Yes, the Local-For is even much faster than the default For but it has two restrictions: (1) The iterator variable is only valid inside the loop. (2) You must not write to the iterator variable. It is read-only. See chapter 8.3 in the Hollywood guide for a more detailed explanation and also some examples.
User avatar
airsoftsoftwair
Posts: 5914
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[29 Sep 2008] Re: Re: Possible bug in For...Next loop

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 29 Sep 2008 00:37:46 +0200

YES, it's definitely a bug. The Non-Local-For statement does not seem to work correctly with recursive functions. Thanks for reporting!
Locked