Page 1 of 1

Moving Element from one list to a other

Posted: Wed Jul 10, 2013 4:46 pm
by Josh
Would be nice to have a native way to move an element from one list to a other. Doing it by hand, is a little bit complicated.

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:06 pm
by DK_PETER
? What's wrong with CopyStructure()?

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:09 pm
by Josh
DK_PETER wrote:? What's wrong with CopyStructure()?
Pointers to the listelement are lost in this case

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:31 pm
by DK_PETER
Hmmm..Okay..
How about this?

Code: Select all

Declare CopyTheElement(*src, *dest)

Structure ELEMENT_DATA
  id.i
  num.i
  var.s
EndStructure

Global NewList List1.ELEMENT_DATA()
Global NewList List2.ELEMENT_DATA()

Procedure CopyTheElement(*src, *dest)
  CopyStructure(*src, *dest, ELEMENT_DATA)
EndProcedure


For x = 0 To 100
  AddElement(List1())
  List1()\id = Random(255,10)
  List1()\num = Random(1000,100)
  List1()\var = "This is a string " + Str(x)
Next x

SelectElement(List1(),55)
AddElement(List2())
CopyTheElement(@List1(), @List2())

Debug Str(List2()\id)
Debug List2()\var
Wouldn't that work?

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:35 pm
by STARGĂ…TE
CopyStructure is to slow for big list elements.

MoveElement() change only the next- and previous element pointer, but only in one list.
PB 4.60 B1 - why MoveElement() isn't across lists?

We need some like TransferElement(SourceList(), DestinationList(), Location [, *RelativeElement])

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:40 pm
by Demivec
You could section a larger list into two portions and keep track of where they start and stop. Then you only have to use MoveElement() to make the transition between the two 'logical' lists.

Sorting can still be done by specifying the start and end for the list 'portion' you were interested in and you could iterate through their elements by using While/Wend or Repeat/Until instead of a ForEach/Next.

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 5:44 pm
by Josh
There is a way, but you have to create a temporary list and then you have to do this:

Code: Select all

ChangeCurrentElement (List1(), *Element)
MoveElement          (List1(), #PB_List_Last)
SplitList            (List1(), ListTemp())
MergeLists           (ListTemp(), List2)
I think Pb should do this by an easier native way.

Re: Moving Element from one list to a other

Posted: Wed Jul 10, 2013 6:35 pm
by Demivec
Josh wrote:There is a way, but you have to create a temporary list and then you have to do this:

Code: Select all

ChangeCurrentElement (List1(), *Element)
MoveElement          (List1(), #PB_List_Last)
SplitList            (List1(), ListTemp())
MergeLists           (ListTemp(), List2)
I think Pb should do this by an easier native way.
That's a great idea.