Copy List Element

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Copy List Element

Post by IdeasVacuum »

We have Copy List, works like a charm.

Would be very nice to have Copy List Element - especially for Structured Lists.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Copy List Element

Post 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()
DE AA EB
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Copy List Element

Post 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()

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
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Copy List Element

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Copy List Element

Post by davido »

@mk-soft,
Thank you for the excellent Macro. :D
I'll be using this instead of my rather crude method.
DE AA EB
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Copy List Element

Post 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).
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Copy List Element

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Copy List Element

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Copy List Element

Post 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.
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
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Copy List Element

Post 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).
Image
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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Copy List Element

Post 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
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
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Copy List Element

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Copy List Element

Post 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
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
Post Reply