Copy List Element
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Copy List Element
We have Copy List, works like a charm.
Would be very nice to have Copy List Element - especially for Structured Lists.
Would be very nice to have Copy List Element - especially for Structured Lists.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Copy List Element
@IdeasVacuum,
I've used this code; would this work for you?
I've used this code; would this work for you?
Code: Select all
Structure az
Coin$
Pence.i
EndStructure
Global NewList a.az(), NewList b.az()
Global *aPTR, *bPTR
With a()
AddElement(a())
\Coin$ = "Florin"
\Pence = 24
AddElement(a())
\Coin$ = "Shilling"
\Pence = 12
EndWith
AddElement(b())
With b()
\Coin$ = "Half Crown"
\Pence = 30
EndWith
ForEach a()
With a()
Debug \Coin$ + " - " + \Pence
EndWith
Next a()
*aPTR = @a()
AddElement(b())
*bPTR = @b()
CopyStructure(*aPTR,*bPTR,az)
Debug "___________"
ForEach b()
With b()
Debug \Coin$ + " - " + \Pence
EndWith
Next b()
DE AA EB
Re: Copy List Element
You don't need pointers.
DestListElement() = SourceListElement()
DestListElement() = SourceListElement()
Code: Select all
Macro CopyListElement(Element, DestList, Location=#PB_List_Last)
CompilerSelect Location
CompilerCase #PB_List_After
AddElement(DestList) : DestList = Element
CompilerCase #PB_List_Before
InsertElement(DestList) : DestList = Element
CompilerCase #PB_List_First
ResetList(DestList) : AddElement(DestList) : DestList = Element
CompilerCase #PB_List_Last
LastElement(DestList) : AddElement(DestList) : DestList = Element
CompilerEndSelect
EndMacro
Structure az
Coin$
Pence.i
List Name.s()
EndStructure
Global NewList a.az(), NewList b.az()
With a()
AddElement(a())
\Coin$ = "Florin"
\Pence = 24
AddElement(\Name())
\Name() = "100"
AddElement(\Name())
\Name() = "200"
AddElement(a())
\Coin$ = "Shilling"
\Pence = 12
EndWith
AddElement(b())
With b()
\Coin$ = "Half Crown"
\Pence = 30
EndWith
Debug "*** List a()"
ForEach a()
With a()
Debug \Coin$ + " - " + \Pence
ForEach \Name() : Debug \Name() : Next
EndWith
Next a()
Debug "*** Do copy and change"
SelectElement(a(), 0)
CopyListElement(a(), b(), #PB_List_First)
ClearList(a()\Name())
AddElement(a()\Name())
a()\Name() = "999"
AddElement(b()\Name())
b()\Name() = "300"
Debug "*** List a()"
ForEach a()
With a()
Debug \Coin$ + " - " + \Pence
ForEach \Name() : Debug \Name() : Next
EndWith
Next a()
Debug "*** List b()"
ForEach b()
With b()
Debug \Coin$ + " - " + \Pence
ForEach \Name() : Debug \Name() : Next
EndWith
Next b()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Copy List Element
Hi mk-soft
I do of course know how to copy, but I think a PB Function for the task should be available. I copy Elements more often than I copy whole Lists.
I do of course know how to copy, but I think a PB Function for the task should be available. I copy Elements more often than I copy whole Lists.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Copy List Element
@mk-soft,
Thank you for the excellent Macro.
I'll be using this instead of my rather crude method.
Thank you for the excellent Macro.
I'll be using this instead of my rather crude method.
DE AA EB
Re: Copy List Element
You mean like this?
a() returns the address of the current structured element, AddElement(b()) also returns the address (of the new element).
Code: Select all
CopyStructure(a(), AddElement(b()), structure)
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Copy List Element
Hi Guys, thanks for all the interest shown in this post. Sorry I missed you out first time around davido!
@Trond
Something like this pseudo code:
@Trond
Something like this pseudo code:
Code: Select all
NewList MyList01.BigishStructure()
NewList MyList02.BigishStructure()
;Add Data Elements to List01. List02 is empty, then later:
ForEach MyList01()
If (MyList01()\dMagicNumber = 3.333333)
CopyElement(MyList01(), MyList02()) ;New Element automatically added to List02
EndIf
Next
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Copy List Element
That is exactly what my code does.
Code: Select all
NewList MyList01.BigishStructure()
NewList MyList02.BigishStructure()
;Add Data Elements to List01. List02 is empty, then later:
ForEach MyList01()
If (MyList01()\dMagicNumber = 3.333333)
;New Element automatically added to List02
CopyStructure(MyList01(), AddElement(MyList02()), BigishStructure)
EndIf
Next
Re: Copy List Element
@Trond
You do not need to specify the structure. Purebasic does that automatically.
Besides you don't know where the element will be added. Beginning, end, middle?
For IdeasVacuum once the macro name changed!
If you need the native in Purebasic, or if you create the macro yourself, I don't know exactly.
You do not need to specify the structure. Purebasic does that automatically.
Besides you don't know where the element will be added. Beginning, end, middle?
For IdeasVacuum once the macro name changed!
Code: Select all
Macro CopyElement(ActualElement, DestList, Location=#PB_List_Last)
CompilerSelect Location
CompilerCase #PB_List_After
AddElement(DestList) : DestList = Element
CompilerCase #PB_List_Before
InsertElement(DestList) : DestList = Element
CompilerCase #PB_List_First
ResetList(DestList) : AddElement(DestList) : DestList = ActualElement
CompilerCase #PB_List_Last
LastElement(DestList) : AddElement(DestList) : DestList = ActualElement
CompilerEndSelect
EndMacro
Structure udtData
iVal.i
sVal.s
EndStructure
NewList MyList01.udtData()
NewList MyList02.udtData()
AddElement(MyList01())
MyList01()\iVal = 1
MyList01()\sVal = "Hello World!"
AddElement(MyList01())
MyList01()\iVal = 2
MyList01()\sVal = "I Like PureBasic!"
ForEach MyList01()
If MyList01()\iVal = 2
CopyElement(MyList01(), MyList02())
EndIf
Next
ForEach MyList02()
Debug MyList02()\sVal
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Copy List Element
Trond wrote:You mean like this?a() returns the address of the current structured element, AddElement(b()) also returns the address (of the new element).Code: Select all
CopyStructure(a(), AddElement(b()), structure)
This is known, of course.mk-soft wrote:Besides you don't know where the element will be added. Beginning, end, middle?
Help for AddElement() wrote:Adds a new empty element after the current element or as the first item in the list if there are no elements in it.
Re: Copy List Element
@Little John
I know that too,
but I wanted to have the function 'CopyElement(SourceElement(), TargetList() [, Location])' exactly like the function 'MergeLists(SourceList(), TargetList() [, Location])
But I think everyone can build such macros themselves.
I know that too,
but I wanted to have the function 'CopyElement(SourceElement(), TargetList() [, Location])' exactly like the function 'MergeLists(SourceList(), TargetList() [, Location])
But I think everyone can build such macros themselves.
Code: Select all
;-TOP
Macro CopyElement(SourceElement, TargetList, Location=#PB_List_Last)
CompilerSelect Location
CompilerCase #PB_List_After
AddElement(TargetList) : TargetList = SourceElement
CompilerCase #PB_List_Before
InsertElement(TargetList) : TargetList = SourceElement
CompilerCase #PB_List_First
ResetList(TargetList) : AddElement(TargetList) : TargetList = SourceElement
CompilerCase #PB_List_Last
LastElement(TargetList) : AddElement(TargetList) : TargetList = SourceElement
CompilerEndSelect
EndMacro
Macro MoveElement(SourceElement, TargetList, Location=#PB_List_Last)
CompilerSelect Location
CompilerCase #PB_List_After
AddElement(TargetList) : TargetList = SourceElement : DeleteElement(SourceElement)
CompilerCase #PB_List_Before
InsertElement(TargetList) : TargetList = SourceElement : DeleteElement(SourceElement)
CompilerCase #PB_List_First
ResetList(TargetList) : AddElement(TargetList) : TargetList = SourceElement : DeleteElement(SourceElement)
CompilerCase #PB_List_Last
LastElement(TargetList) : AddElement(TargetList) : TargetList = SourceElement : DeleteElement(SourceElement)
CompilerEndSelect
EndMacro
; ----
Structure udtData
iVal.i
sVal.s
EndStructure
NewList MyList01.udtData()
NewList MyList02.udtData()
AddElement(MyList01())
MyList01()\iVal = 1
MyList01()\sVal = "Hello World!"
AddElement(MyList01())
MyList01()\iVal = 2
MyList01()\sVal = "I Like PureBasic!"
AddElement(MyList01())
MyList01()\iVal = 3
MyList01()\sVal = "Powered by Purebasic!"
AddElement(MyList01())
MyList01()\iVal = 4
MyList01()\sVal = "End!"
ForEach MyList01()
If MyList01()\iVal = 2
CopyElement(MyList01(), MyList02())
ElseIf MyList01()\iVal = 3
MoveElement(MyList01(), MyList02(), #PB_List_First)
EndIf
Next
Debug "List 1"
ForEach MyList01()
Debug MyList01()\sVal
Next
Debug "List 2"
ForEach MyList02()
Debug MyList02()\sVal
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Copy List Element
That's fine, but it wasn't about your personal preferences.mk-soft wrote:but I wanted to have ...
The point was that you made a false statement about Trond's code that needed correcting.
Re: Copy List Element
Wasn't my intention either.
I just wanted to say that, because you have to set the current target element yourself and specify the structure.
Sorry
I just wanted to say that, because you have to set the current target element yourself and specify the structure.
Sorry
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive