Shouldn't this work?

Just starting out? Need help? Post your questions and find answers here.
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Shouldn't this work?

Post by CSAUER »

for my understanding, this piece of code should work correctly:

Code: Select all

Global temp.s
NewList mylist.s()

AddElement(mylist())
mylist() = "John"
AddElement(mylist())
mylist() = "Eric"

  ResetList(mylist())  
  While NextElement(mylist()) 
    temp = mylist()
    *Old_Element = @mylist()  
    LastElement(mylist())
    AddElement(mylist())
    mylist() = temp + "-copy"
    ChangeCurrentElement(mylist(),*Old_Element)
  Wend 
I expect to have following values after running this code:
- John
- Eric
- John-Copy
- Eric-Copy

But it remains in an endless loop. Any idea what's going on?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PB4.1 - Win: MacBook black 2008 2,4 GHz, 4 GB RAM, MacOSX 10.5/VMWare/WinXP
PB4.1 - Mac: MacMini G4 1,4 GHz, 512 MB RAM, MacOSX 10.4
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Well, by adding an extra element AFTER THE CURRENT ELEMENT in each iteration of the loop, you are thus ensuring that the end of the list is never reached! NextElement(mylist()) (in the While clause) will always return a non-zero result in your code!

You will have to rethink what it is you are trying to do.

**EDIT : try this :

Code: Select all

Global temp.s 
NewList mylist.s() 

AddElement(mylist()) 
mylist() = "John" 
AddElement(mylist()) 
mylist() = "Eric" 

  ResetList(mylist())  
  count = CountList(mylist())
  While i < count 
    NextElement(mylist()) 
    temp = mylist() 
    *Old_Element = @mylist()  
    LastElement(mylist()) 
    AddElement(mylist()) 
    mylist() = temp + "-copy" 
    ChangeCurrentElement(mylist(),*Old_Element) 
    i+1
  Wend 
  
I may look like a mule, but I'm not a complete ass.
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

Thanks srod. My purpose is to do a copy of a complexe linked list and append the copy at the end.

My idea was:
1. read out the actual list item
2. remember the pointer
3. add a new list item
4. go back to the pointer and continue looping

A workaround could be to copy existing linked list into a temporary local one and append the new stuff in a second step. But my attemp was to do it within one step. :D

*edit*: Your solution runs. Thanks. Optimized version:

Code: Select all

Global temp.s
NewList mylist.s()

AddElement(mylist())
mylist() = "John"
AddElement(mylist())
mylist() = "Eric"

  ResetList(mylist()) 
  count = CountList(mylist())
  For i = 1 To count
    NextElement(mylist())
    temp = mylist()
    *Old_Element = @mylist() 
    LastElement(mylist())
    AddElement(mylist())
    mylist() = temp + "-copy"
    ChangeCurrentElement(mylist(),*Old_Element)
  Next
Last edited by CSAUER on Thu May 29, 2008 4:03 pm, edited 3 times in total.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PB4.1 - Win: MacBook black 2008 2,4 GHz, 4 GB RAM, MacOSX 10.5/VMWare/WinXP
PB4.1 - Mac: MacMini G4 1,4 GHz, 512 MB RAM, MacOSX 10.4
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Re: Shouldn't this work?

Post by Pupil »

Insert the elements above the current element, and you'll se that the loop exits. Another way would be to get the address of the last element before entering loop and check if the current element equals this. If it does you should exit the loop.
As it is now youl'll get the following added to the list:
- John
- Eric
- John-Copy
- Eric-Copy
- John-Copy-Copy
- Eric-Copy-Copy
- ... etc
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

Oh yes, sorry I was some kind of stupid. You are right. Such a beginner question... :oops:
I think it is much to hot today - 27°C in the room.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PB4.1 - Win: MacBook black 2008 2,4 GHz, 4 GB RAM, MacOSX 10.5/VMWare/WinXP
PB4.1 - Mac: MacMini G4 1,4 GHz, 512 MB RAM, MacOSX 10.4
Post Reply