It is currently Fri Jun 22, 2018 6:45 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 12:34 pm 
Offline
User
User

Joined: Fri May 16, 2003 7:17 pm
Posts: 83
Hello,

Watch this code:

Code:
EnableExplicit

Structure MyStructure
  A.s
  B.s
EndStructure

Procedure Function(String.s)
  Debug "loop"
EndProcedure

Procedure Test()
  Protected NewMap MyMap.MyStructure()

  MyMap("0")\A = ""
  ForEach MyMap()
    Function(MyMap(MyMap()\A)\B)
  Next
EndProcedure

Test()


It makes an infinite loop only if you call a function inside ForEach. I can't explain why it happens. :shock:

Bye.


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 12:50 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1136
Location: Germany, Glienicke
It is very easy to explain:

When ever you call a map element, it will be set as the current element in the ForEach loop.

At the beginning you have only one element: MyMap("0")
In the ForEach-loop you create an addition element: MyMap("") (MyMap("0")\A = "" and you call MyMap(MyMap("0")\A))
But this element will plance "befor" MyMap("0").

So, when ever the ForEach-loop is at element MyMap("0") you inner call MyMap(MyMap("0")\A) set the current element back to MyMap("")

So no bug, but a misuse of ForEach!

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 2:37 pm 
Offline
User
User

Joined: Fri May 16, 2003 7:17 pm
Posts: 83
It's not as easy as you think because if you remove function call, there is no infinite loop ... :?
Behaviour should not change with or without function call : the bug is here.


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 2:50 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3485
Location: Utah, USA
It's not a bug.

Make some small changes in your loop.
Code:
EnableExplicit

Structure MyStructure
  A.s
  B.s
EndStructure

Procedure Function(String.s)
  Debug "loop"
EndProcedure

Procedure Test()
  Protected NewMap MyMap.MyStructure()

  MyMap("0")\A = ""
  ForEach MyMap()
    PushMapPosition(MyMap())
    Function(MyMap(MyMap()\A)\B)
    PopMapPosition(MyMap())
  Next
EndProcedure

Test()

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 3:34 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1136
Location: Germany, Glienicke
Phlos wrote:
It's not as easy as you think because if you remove function call, there is no infinite loop ... :?
Behaviour should not change with or without function call : the bug is here.

What do you mean with "remove function call"?

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 7:25 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 670
STARGÅTE wrote:
What do you mean with "remove function call"?
I think Phlos means, if he's use
Code:
Debug MyMap (MyMap ()\A)\B
instead of
Code:
Function(MyMap(MyMap()\A)\B)
no endless loop is created.

Phlos wrote:
It's not as easy as you think because if you remove function call, there is no infinite loop ... :?
Behaviour should not change with or without function call : the bug is here.
It's no bug. You are using the map in a wrong way, so don't be surprised if you get unpredictable results.

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: [5.62] Infinite loop in Map
PostPosted: Sun Feb 11, 2018 8:05 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1136
Location: Germany, Glienicke
ah ok, but this is indeed inconsistent, as you can see here:
Code:
Define NewMap MyMap.s()

Procedure ViewMapElements(Map MyMap.s())
   If MapSize(MyMap())
      ForEach MyMap()
         Debug "   MyMap("+MapKey(MyMap())+") = "+MyMap()
      Next
   Else
      Debug "   Empty"
   EndIf
EndProcedure



Debug "Get Element:"
ClearMap(MyMap())
Define String.s = MyMap("A")
ViewMapElements(MyMap())

Debug "Get Element in Function:"
ClearMap(MyMap())
Define Integer.i = Val(MyMap("A"))
ViewMapElements(MyMap())

Debug "Get Element in Operation:"
ClearMap(MyMap())
Define String.s = MyMap("A") + MyMap("B")
ViewMapElements(MyMap())

Debug "Get Element in Function:"
ClearMap(MyMap())
FindString(MyMap("A"), MyMap("B"))
ViewMapElements(MyMap())

Debug "Get Element in If:"
ClearMap(MyMap())
If MyMap("A")
EndIf
ViewMapElements(MyMap())


Further topics: Functions PB adding itself MapElements

_________________
ImageImage


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye