Page 1 of 1
Copy List Element
Posted: Sat Jan 11, 2020 7:34 pm
by IdeasVacuum
We have Copy List, works like a charm.
Would be very nice to have Copy List Element - especially for Structured Lists.
Re: Copy List Element
Posted: Sat Jan 11, 2020 8:11 pm
by davido
@
IdeasVacuum,
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()
Re: Copy List Element
Posted: Sun Jan 12, 2020 12:04 am
by mk-soft
You don't need pointers.
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()
Re: Copy List Element
Posted: Sun Jan 12, 2020 1:44 am
by IdeasVacuum
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.
Re: Copy List Element
Posted: Sun Jan 12, 2020 4:26 pm
by davido
@
mk-soft,
Thank you for the excellent Macro.
I'll be using this instead of my rather crude method.
Re: Copy List Element
Posted: Sun Jan 12, 2020 10:42 pm
by Trond
You mean like this?
Code: Select all
CopyStructure(a(), AddElement(b()), structure)
a() returns the address of the current structured element, AddElement(b()) also returns the address (of the new element).
Re: Copy List Element
Posted: Mon Jan 13, 2020 2:12 pm
by IdeasVacuum
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:
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
Re: Copy List Element
Posted: Mon Jan 13, 2020 9:10 pm
by Trond
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
Posted: Mon Jan 13, 2020 10:07 pm
by mk-soft
@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!
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
If you need the native in Purebasic, or if you create the macro yourself, I don't know exactly.
Re: Copy List Element
Posted: Wed Jan 15, 2020 4:07 am
by Little John
Trond wrote:You mean like this?
Code: Select all
CopyStructure(a(), AddElement(b()), structure)
a() returns the address of the current structured element, AddElement(b()) also returns the address (of the new element).
mk-soft wrote:Besides you don't know where the element will be added. Beginning, end, middle?
This
is known, of course.
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
Posted: Wed Jan 15, 2020 9:30 am
by mk-soft
@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.
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
Re: Copy List Element
Posted: Wed Jan 15, 2020 6:42 pm
by Little John
mk-soft wrote:but I wanted to have ...
That's fine, but it wasn't about your personal preferences.
The point was that you made a false statement about Trond's code that needed correcting.
Re: Copy List Element
Posted: Wed Jan 15, 2020 7:26 pm
by mk-soft
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