Problem using CopyStructure

Just starting out? Need help? Post your questions and find answers here.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Problem using CopyStructure

Post by Paul »

This works fine...

Code: Select all

Structure testlist
  name.i
  flag.i
EndStructure

test.testlist
tmp.testlist

tmp\name=21
Debug tmp\name
Debug "----------"

CopyStructure(@tmp,@test.testlist,testlist)

Debug test\name
But can you do the same with Structure and List?

Code: Select all

Structure testlist
  name.i
  flag.i
EndStructure

NewList test.testlist()
NewList tmp.testlist()

AddElement(tmp()):tmp()\name=21
AddElement(tmp()):tmp()\name=42
AddElement(tmp()):tmp()\name=69
AddElement(tmp()):tmp()\name=99

ForEach tmp()
  Debug tmp()\name
Next
Debug "----------"

CopyStructure(@tmp(),@test(),testlist)  ;<--------- This doesn't work, what is the proper syntax?

ForEach test()
  Debug test()\name
Next
Image Image
Micoute
User
User
Posts: 24
Joined: Sat Jun 22, 2013 4:06 pm
Location: La Mézière FRANCE

Re: Problem using CopyStructure

Post by Micoute »

Code: Select all

Structure testlist
  name.i
  flag.i
EndStructure

NewList test.testlist()
NewList tmp.testlist()

AddElement(tmp()):AddElement(test()):tmp()\name=21
AddElement(tmp()):AddElement(test()):tmp()\name=42
AddElement(tmp()):AddElement(test()):tmp()\name=69
AddElement(tmp()):AddElement(test()):tmp()\name=99

ForEach tmp()
  Debug tmp()\name
Next
Debug "----------"

CopyStructure(@tmp(),@test(),testlist)  ;<--------- This doesn't work, what is the proper syntax?

ForEach test()
  Debug test()\name
Next
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Problem using CopyStructure

Post by Marc56us »

CopyStructure() copies the elements one by one
1. Need to copy each element.
2. Target element must exist.

Code: Select all

Structure testlist
  name.i
  flag.i
EndStructure

NewList test.testlist()
NewList tmp.testlist()

AddElement(tmp()):tmp()\name=21
AddElement(tmp()):tmp()\name=42
AddElement(tmp()):tmp()\name=69
AddElement(tmp()):tmp()\name=99

ForEach tmp()
  Debug tmp()\name
Next
Debug "----------"

; OLD
; CopyStructure(@tmp(),@test(),testlist)  ;<--------- This doesn't work, what is the proper syntax?

; NEW
ForEach tmp()
    AddElement(test())
    CopyStructure(@tmp(), @test(), testlist)
Next

ForEach test()
  Debug test()\name
Next
Tested.

Code: Select all

21
42
69
99
----------
21
42
69
99
PS. Micoute's solution works, but copie only last element.
Work fine like this

Code: Select all

; CopyStructure(@tmp(),@test(),testlist)

ResetList(test())
ForEach tmp()
    NextElement(test())
    CopyStructure(@tmp(),@test(),testlist)
Next
But at end, think simple...

Code: Select all

; CopyStructure(@tmp(),@test(),testlist)  ;<--------- This doesn't work, what is the proper syntax?
CopyList(tmp(), test())
:wink:
infratec
Always Here
Always Here
Posts: 6866
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem using CopyStructure

Post by infratec »

When you want to copy lists, why you don't use the right command:

Code: Select all

CopyList()
:?:

Bernd
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem using CopyStructure

Post by Paul »

Marc56us wrote: But at end, think simple...

Code: Select all

; CopyStructure(@tmp(),@test(),testlist)  ;<--------- This doesn't work, what is the proper syntax?
CopyList(tmp(), test())
:wink:
Dohhh !! :oops:

Was so focused on the structure part, didn't think of the list.
And some speed test show CopyList() is a bit faster than looping through with CopyStructure()

Thanks !
Image Image
Post Reply