# PureBasic Forum

 It is currently Sat Mar 17, 2018 5:36 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 12:34 pm
 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.

Bye.

Top

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 12:50 pm

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1129
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!

_________________

Top

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 2:37 pm
 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

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 2:50 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3450
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()

_________________

Top

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 3:34 pm

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1129
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"?

_________________

Top

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 7:25 pm
 Enthusiast

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 624
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
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.

_________________

Top

 Post subject: Re: [5.62] Infinite loop in MapPosted: Sun Feb 11, 2018 8:05 pm

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1129
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

_________________

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite