add element to structure

Byak@

Active Member
Licensed User
hello all.
i'm want to add a new element in existing structure but a can't...

i'm try this code
B4X:
Sub Globals
Dim Type(s1,s2)var(0)
Dim Type(s1,s2)var2(0)
End Sub

Sub App_Start
var()=Array((1,2),(2,3))

Dim var2(ArrayLen(var()),ArrayLen(var(),2))
var2()=var()
'ArrayCopy(var(),0,ArrayLen(var()),var2(),0)
Dim var(ArrayLen(var2())+1,ArrayLen(var(),2))
ArrayCopy(var2(),0,ArrayLen(var2()),var(),0)
'var()=var2()
var(ArrayLen(var())-1).s1=3
var(ArrayLen(var())-1).s2=4

For i=0 To ArrayLen(var())-1
s=s&var(i).s1
Next
Msgbox(ArrayLen(var()),s)

End Sub
but it doesnot work correct...
 

mjcoon

Well-Known Member
Licensed User
i'm want to add a new element in existing structure but a can't.

I'm not surprised! Adding an element to a structure involves adding a new name. Just changing the size of each dimension of a 2-D array is much simpler.

The help for ArrayCopy() does not say that it is only for 1-D arrays, but I'm not sure what the "position" parameter means for 2-D arrays.

...but it doesnot work correct...

It's always more useful to say what it does do rather than what it does not (especially if you do not explain what "correct" would be like!)...

Mike.
 

agraham

Expert
Licensed User
Longtime User
Assuming that you mean you want to change Type(s1, s2)var(x) to Type(s1, s2, s3)var(x) use Dim var(ArrayLen(var(),1), ArrayLen(var(),2)+1). However you will have to address the extra element(s) as a normal array.

B4X:
Sub Globals
Dim Type(s1,s2,s3,s4)var(2)
End Sub

Sub App_Start
   Dim var(ArrayLen(var(),1),ArrayLen(var(),2)+1)
   Msgbox(ArrayLen(var(),1) & " " & ArrayLen(var(),2),s)
   var(0).s1 = 0
   var(0).s2 = 1
   var(0).s3 = 2
   var(0).s4 = 3
   var(0, 3) = 4
   For i = 0 To ArrayLen(var(),2) - 1
      msg = msg & " " & var(0, i)
   Next
   Msgbox(msg)
End Sub
Not sure why you would want to this instead of using normal array access

EDIT:- ArrayCopy does work on multi-dimensional arrays. It treats the arrays as single dimensioned arrays of a total length of all the array dimensions multiplied together so you can get "interesting" results by varying the source and target start indices.
B4X:
' copies all the elements of var to var2
ArrayCopy(var(), 0 , ArrayLen(var(),1)*ArrayLen(var(),2), var2(), 0)
 
Last edited:

Byak@

Active Member
Licensed User
oh, you don't understand me (sorry for my bad english)
for example we have a structure type(s1,s2)var(1)
0 1
and we want to add a new rows and new structure must be
0 1
1 2
2 3

tonight i'm create code,doing it
B4X:
Sub Globals
Dim Type(s1,s2)var1(1) As double
Dim var2(0)
End Sub

Sub App_Start
'our array structure
var1(0,0)=0
var1(0,1)=1
showstructure
'adding new rows
Dim var2(ArrayLen(var1(),1),ArrayLen(var1(),2))
var2()=var1()
'copy old values
Dim var1(ArrayLen(var2(),1)+2,ArrayLen(var2(),2))
For i=0 To ArrayLen(var2(),1)-1
For j=0 To ArrayLen(var2(),2)-1
var1(i,j)=var2(i,j)
Next
Next
'add new rows
var1(ArrayLen(var1(),1)-2,0)=1
var1(ArrayLen(var1(),1)-2,1)=2
var1(ArrayLen(var1(),1)-1,0)=2
var1(ArrayLen(var1(),1)-1,1)=3
showstructure
End Sub

Sub showstructure
For i=0 To ArrayLen(var1(),1)-1
For j=0 To ArrayLen(var1(),2)-1
s=s&var1(i,j)&"    "
Next
s=s&CRLF
Next
Msgbox(s)
End Sub
and now it work good.
 

agraham

Expert
Licensed User
Longtime User
I suspect you are still using v6.80 because the above code won't work for me. In v6.90 you can't change the rank or type of an array so var2 must be declared and redimmed as a Double array of rank 2.

Dim var2(0,0) As double

You can use ArrayCopy which will be much much faster than looping.
B4X:
Sub Globals
Dim Type(s1,s2)var1(1) As double
Dim var2(0[COLOR="red"],0) As double[/COLOR]
End Sub

Sub App_Start
'our array structure
var1(0,0)=0
var1(0,1)=1
showstructure
'adding new rows
Dim var2(ArrayLen(var1(),1),ArrayLen(var1(),2))[COLOR="Red"] As Double[/COLOR]
var2()=var1()
'copy old values
Dim var1(ArrayLen(var2(),1)+2,ArrayLen(var2(),2)) [COLOR="red"]As Double[/COLOR]
[COLOR="Red"]ArrayCopy(var2(), 0, ArrayLen(var2(),1)*ArrayLen(var2(),2), var1(), 0)[/COLOR]
'add new rows
var1(ArrayLen(var1(),1)-2,0)=1
var1(ArrayLen(var1(),1)-2,1)=2
var1(ArrayLen(var1(),1)-1,0)=2
var1(ArrayLen(var1(),1)-1,1)=3
showstructure
End Sub

Sub showstructure
For i=0 To ArrayLen(var1(),1)-1
For j=0 To ArrayLen(var1(),2)-1
s=s&var1(i,j)&"    "
Next
s=s&CRLF
Next
Msgbox(s)
End Sub
 

Byak@

Active Member
Licensed User
Thanks Andrew)
yes i'm use a 6.8
 

mjcoon

Well-Known Member
Licensed User

I tried this (because it is simpler to have a single one-dimension temporary array for copying purposes rather than match each permanent one). But with 6.90 I get a error message (in desktop IDE) that the dimensionality of source and target arrays must match.

Mike.
 

agraham

Expert
Licensed User
Longtime User
I get a error message (in desktop IDE) that the dimensionality of source and target arrays must match.Mike.
Yes, I can't see your problem as you haven't shown the rank of each array. But as I stated ArrayCopy does work with multi-dimensional arrays and the code I posted above in post #5 does work in 6.90 as does this.

B4X:
Sub Globals
   'Declare the global variables here.
   Dim a(4,5)
   Dim b(4,5)
End Sub

Sub App_Start
   For i = 0 To 3
      For j = 0 To 4
         a(i, j) = i*5 + j
      Next
   Next
   ArrayCopy(a(), 0, 20, b(), 0)
   Msgbox(b(3, 4))   
End Sub
 

mjcoon

Well-Known Member
Licensed User
Yes, I can't see your problem as you haven't shown the rank of each array.

True. It was your saying
It treats the arrays as single dimensioned arrays
that made me think that the dimensionality was going to be ignored, hence my falling into that trap.

The place for a full explanation would be/have been the Help text for ArrayCopy()...

Mike.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…