Insert element in structured array [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Insert element in structured array [Resolved]

Post by Kwai chang caine »

Hello at all

Started from a nice code of Michael vogel for insert an element in String array
http://www.purebasic.fr/english/viewtop ... 40#p369940

I have try to modify it for insert this time an element in a strutrured array

Code: Select all

Structure Try
 a.i
 b.s
 c.i
EndStructure

Procedure ArrayInsertStructure(Array name.try(1), Pos, *Element.try)

 Protected n = ArraySize(name()) + 1
 ReDim name(n)
 name(n)\a = *Element\a
 name(n)\b = *Element\b
 name(n)\c = *Element\c
 
 n = PeekL(name() + SizeOf(try) * n)
 MoveMemory(name() + SizeOf(try) * Pos - SizeOf(try), name() + SizeOf(try) * Pos, PeekL(name() - 8) * SizeOf(try) - SizeOf(try) * Pos)
 PokeL(name() + SizeOf(try) * Pos, n)

EndProcedure

Dim s.Try(10)
Define Element.Try

For i = 1 To 10
 s(i)\a = i
 s(i)\b = "String" + Trim(Str(i))
 s(i)\c = Val(Trim(Str(i)) + Trim(Str(i)))
Next

Element\a = 100
Element\b = "Hello"
Element\c = 1000
 
ArrayInsertStructure(s(), 5, @Element)

For i = 1 To ArraySize(s())
 Debug s(i)\a
 Debug s(i)\b
 Debug s(i)\c
 Debug ""
Next i
But that's works only half :|
The string /b and the /c element is not writed in 5e position
Debugger wrote:1
1
11

2
2
22

3
3
33

4
4
44

100
4
44

5
5
55

6
6
66

7
7
77

8
8
88

9
9
99

10
10
1010
If someone know why ?

Have a good day
Last edited by Kwai chang caine on Wed Sep 28, 2016 5:56 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Insert element in structured array

Post by STARGÅTE »

You move all elements after your new element, but only a Long (why a Long?) into the new position.

Code: Select all

Structure Try
 a.i
 b.s
 c.i
EndStructure

Procedure ArrayInsertStructure(Array name.try(1), Pos, *Element.try)

 Protected n = ArraySize(name()) + 1
 ReDim name(n)
 
 MoveMemory(@name() + SizeOf(try) * (Pos), @name() + SizeOf(try) * (Pos+1), SizeOf(try) * (n-Pos))
 FillMemory(@name() + SizeOf(try) * Pos, SizeOf(try))
 CopyStructure(*Element, @name(Pos), Try)

EndProcedure

Dim s.Try(10)
Define Element.Try

For i = 1 To 10
 s(i)\a = i
 s(i)\b = "String" + Trim(Str(i))
 s(i)\c = Val(Trim(Str(i)) + Trim(Str(i)))
Next

Element\a = 100
Element\b = "Hello"
Element\c = 1000
 
ArrayInsertStructure(s(), 5, @Element)

For i = 1 To ArraySize(s())
 Debug s(i)\a
 Debug s(i)\b
 Debug s(i)\c
 Debug ""
Next i
btw.: an Array starts with index 0, not with 1 !! You have to check your code!
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Insert element in structured array

Post by Kwai chang caine »

Thanks a lot STARGATE !!
That's works perfectly 8)
You move all elements after your new element, but only a Long (why a Long?) into the new position.
In fact, the code of Michael is too strong for my little head :oops:
I have believe that replace the 4 of string adress by the SizeOf(try) is enough :oops:

Code: Select all

MoveMemory(name()+4*element-4,name()+4*element,PeekL(name()-8)*4-4*element)
And for the "8" i don't understand why Michael write it :?:

I have do what i can..so nearly nothing, and the great STARGATE do the rest :mrgreen:

During my wait, i have all copy in another array with the new line
Clear the old
Copy the new array in the old
Delete the new array
It's that my level :oops:

Again thanks for your precious help
Have a very good day
ImageThe happiness is a road...
Not a destination
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Insert element in structured array [Resolved]

Post by Lunasole »

Can also use Lists instead of writing additional code. That's even better than array for most cases, as you don't have to define additional counters, etc

Code: Select all

EnableExplicit

; like array of strings
Global NewList Test$ ()

; add 3 dummy items
AddElement(Test$())
Test$() = "1"
AddElement(Test$())
Test$() = "2"
AddElement(Test$())
Test$() = "3"

; now insert item after item #2
SelectElement(Test$(), 2)
InsertElement(Test$())
Test$() = "2.5"


; simply draw list content from 1 to last item
ForEach Test$()
	Debug Test$()
Next
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Insert element in structured array [Resolved]

Post by collectordave »

Looking at the same problem.

Inspired by this post for deleting an element.

http://www.purebasic.fr/english/viewtop ... ay+element

Which does it with a macro

Code: Select all

Macro deleteArrayElement(ar, el)
 
  For a=el To ArraySize(ar())-1
    ar(a) = ar(a+1)
  Next 
 
  Redim ar(ArraySize(ar())-1)
 
EndMacro
So wrote this which seems to work with structured and normal 1 dimensional arrays

Code: Select all

Structure DTest
  no1.i
  no2.i
  testr.s
EndStructure

Dim Numbers.DTest(5)

For iLoop = 0 To 5
  
  Numbers(iLoop)\no1 = iLoop
  Numbers(iLoop)\no2 = iLoop + 20
  Numbers(iLoop)\testr = Str(iLoop) + " This is text"
  
Next iLoop

NewElement = 4

Macro InsertArrayElement(ar,Pos)

  ReDim ar(ArraySize(ar()) + 1)
  For a = ArraySize(ar()) To Pos Step -1
    If a > 0
      ar(a) = ar(a-1)
    EndIf
  Next a

EndMacro

Macro DeleteArrayElement(ar, el)
 
  For a=el To ArraySize(ar())-1
    ar(a) = ar(a+1)
  Next 
 
  ReDim ar(ArraySize(ar())-1)
 
EndMacro

InsertArrayElement(Numbers, NewElement)

numbers(NewElement)\no1 = 20
numbers(NewElement)\testr = "This has been inserted"

For iLoop = 0 To ArraySize(Numbers())
  
 Debug  Numbers(iLoop)\no1
 Debug  Numbers(iLoop)\testr  
 
Next iLoop
Can anyone see any problems lurking in the background?

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply