Page 1 of 1

Shouldn't this work?

Posted: Thu May 29, 2008 3:39 pm
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?

Posted: Thu May 29, 2008 3:48 pm
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 
  

Posted: Thu May 29, 2008 3:54 pm
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

Re: Shouldn't this work?

Posted: Thu May 29, 2008 3:54 pm
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

Posted: Thu May 29, 2008 3:57 pm
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.