Hello.
I need to do something with 2D arrays, by now i tried this way based on macros:
#chip mega328p, 16
create2Darray( my2Da, 4, 4 ) ' Create 4x4 Array called my2Da
create2Darray( my2Db, 5, 10 )
set2Ddata( my2Da, 3, 2, 100 ) ' Set element 3,2 in my2Da to 100
set2Ddata( my2Db, 2, 9, 217 )
myVar = 0
get2Ddata( my2Da, 3, 2, myVar) ' Get element 3,2 in my2Da and put in myVar
'Now myVar = 100
get2Ddata( my2Db, 2, 9, myVar)
'Now myVar = 217
do
loop
Macro create2Darray( name, dimx, dimy )
Dim name( dimx*dimy )
#define name_ely dimy
End Macro
Macro set2Ddata( name, ordx, ordy, value )
name( (ordx-1)*name_ely+ordy ) = value
End Macro
Macro get2Ddata( name, ordx, ordy, var )
var = name( (ordx-1)*name_ely+ordy )
End Macro
Do you know any other method, easier to use or more intuitive??
the create array macro is ok, but getting and setting elements is a bit complex.
would be nice just doing: my2Da( x, y ) = somevalue or something like that.
Regards.
Last edit: Anobium 2015-10-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I dont think it will work, I based it on zero indexed arrays but I think
GCB is unary indexed and the zero element is size, just rethinking the
problem now.
p.s. Too much beer whilst watching NZ beat my home team South Africa :(
Last edit: Chris Roper 2015-10-24
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When using the method above element 0 of the arrayTestVar will be set to the number of items in the list, which in this case is 9. Each element of the array will then be loaded with the corresponding value in the list - so in the example, TestVar(1) will be set to 1, TestVar(2) to 2, and so on. Element 0 will only be set to number of items in the array when using this method upto 48 data elements.
Array Length
Unless using the above methog element 0 should not be used to obtain the length of the array. Element 0 will only be a consistent with respect to the length of the array ONLY when the array is set as shown above. For all other methods of populate array data see the next paragraph.
The correct, and good, method is to use a constant to set the array size and use the constant within your code to obtain the array length in your code.
#Define ArraySizeConstant 500
Dim TestVar( ArraySizeConstant )
HSerPrint ArraySizeConstant 'or, other usage
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here it is as a function and tested on the PIC16F690, as it uses noting hradware spcific it should run on any GCBasic Target Hardware:
#chip 16f690
#define DimX 5
#define DimY 3
dim MyArray(DimX * DimY)
res = 100
Row = 4
Col = 1
MyArray(FnCell(Row,Col,DimX)) = res
MyVar = MyArray(FnCell(Row,Col,DimX))
if MyVar = res then PortC.0 = 1
end
Function FnCell(Row, Col, Cols)
FnCell = ((Row - 1) * Cols) + Col
end Function
Cheers
Chris
Last edit: Chris Roper 2015-10-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you like, but I think it should have a bit of independent testing first to make sure it holds up under different conditions. If nobody manages to break it then it should be safe.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
try adding spaces in the rows*cols like thai Rows * Cols.
We are also looking at a possible conflict on the AVR as the Rows and Cols
variables have been used in the core libraries
Last edit: Chris Roper 2015-10-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Adapting the code to process the array being defined - please change as follows. I have put the major changes within ****. This should work. The script requires DimX and DimY if you change the constants you will need to change the script.
#chip 16f690
#define DimX 5
#define DimY 3
**dim MyArray( Two_Dimensional_Array )**
dim nextArray (2)
nextArray(1) = 1
res = 100
Row = 4
Col = 1
MyArray(FnCell(Row,Col,DimX)) = res
MyVar = MyArray(FnCell(Row,Col,DimX))
if MyVar = res then PortC.0 = 1
elementtrack = 0
for Row = 1 to Dimy
for Col = 1 to Dimx
MyArray(FnCell(row,col,DimX)) = elementtrack
elementtrack++
HSerPrintByteCRLF MyArray(FnCell(row,col,DimX))
epwrite elementtrack, MyArray(FnCell(row,col,DimX))
wait 250 ms
next
next
end
Function FnCell(Row, Col, Cols)
FnCell = ((Row - 1) * Cols) + Col
end Function
** #script
Two_Dimensional_Array = DimX * DimY
#endscript**
Last edit: Anobium 2015-10-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks Anobium, that script will help in my project.
Just a question: this issue about dim array not accepting constat calculations is the spected behavior? or could be something wrong in my GcBasic build?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Fixed: See https://sourceforge.net/p/gcbasic/discussion/579126/thread/91219dd4/?limit=25&page=1#2670
Hello.
I need to do something with 2D arrays, by now i tried this way based on macros:
Do you know any other method, easier to use or more intuitive??
the create array macro is ok, but getting and setting elements is a bit complex.
would be nice just doing: my2Da( x, y ) = somevalue or something like that.
Regards.
Last edit: Anobium 2015-10-26
Ok... forget the mentioned method... it doesn't work
Does someone know some way to do this?
I havn't tested it but I think this logic might work:
Don't have access to the compiler or any hardware to try it on so I leave
that up to you :)
Cheers
Chris
Last edit: Chris Roper 2015-10-24
Last edit: Chris Roper 2015-10-24
Thanks Chris, i'll give it a try.
I dont think it will work, I based it on zero indexed arrays but I think
GCB is unary indexed and the zero element is size, just rethinking the
problem now.
p.s. Too much beer whilst watching NZ beat my home team South Africa :(
Last edit: Chris Roper 2015-10-24
This posting got me thinking. I have updated the Help to clarify the element zero assumption.
This is section from the lastest help, v0.95. I have references to the usage of element zero .
Setting an entire array at once
It is possible to set several elements of an array with a single line of code. The example below shows how:
Dim TestVar(10)
TestVar = 1, 2, 3, 4, 5, 6, 7, 8, 9
When using the method above element 0 of the arrayTestVar will be set to the number of items in the list, which in this case is 9. Each element of the array will then be loaded with the corresponding value in the list - so in the example, TestVar(1) will be set to 1, TestVar(2) to 2, and so on. Element 0 will only be set to number of items in the array when using this method upto 48 data elements.
Array Length
Unless using the above methog element 0 should not be used to obtain the length of the array. Element 0 will only be a consistent with respect to the length of the array ONLY when the array is set as shown above. For all other methods of populate array data see the next paragraph.
The correct, and good, method is to use a constant to set the array size and use the constant within your code to obtain the array length in your code.
Finally i got the macros workig this way, a bit weird, but is something:
Well I also finaly got my logic working too :)
Have a look and see if it helps
Cheers
Chris
Last edit: Chris Roper 2015-10-24
Here it is as a function and tested on the PIC16F690, as it uses noting hradware spcific it should run on any GCBasic Target Hardware:
Cheers
Chris
Last edit: Chris Roper 2015-10-25
@Chris. Very nice and simle. We should move to the Help File?
If you like, but I think it should have a bit of independent testing first to make sure it holds up under different conditions. If nobody manages to break it then it should be safe.
Cool.. looks good, tomorrow iĺl try it. too late now
Thank you.
Thanks Chris, your code works fine.
Well... looked like it was working, but actually it isn't:
GcBasic creates an array of size 2:
I don't know if the compiler should do it, but the constant preprocessing in Dim myArray is not done.
try adding spaces in the rows*cols like thai Rows * Cols.
We are also looking at a possible conflict on the AVR as the Rows and Cols
variables have been used in the core libraries
Last edit: Chris Roper 2015-10-25
Adapting the code to process the array being defined - please change as follows. I have put the major changes within ****. This should work. The script requires DimX and DimY if you change the constants you will need to change the script.
Last edit: Anobium 2015-10-25
Do not forget to remove the '**!
About the dim issue, adding spaces doesn't help, for avr or pic is the same.
Anyway it's easy to just dim the array to the desired size.
Try https://sourceforge.net/p/gcbasic/discussion/579126/thread/91219dd4/#cc80/9a19 or hard code the size, byt, the script should work for you.
Thanks Anobium, that script will help in my project.
Just a question: this issue about dim array not accepting constat calculations is the spected behavior? or could be something wrong in my GcBasic build?
Nothing wrong.
Is it expected behavior. The order of processing of the source file means that a script will create the correct constant value for you.
I have added two dimensional array to the list of items to be included in the release after v0.95.
I can get it to work Hard coding the Dim But the scipt didn't work for me.
The formula should be DimX * DimY +1 because the zero position is the length of the array, Like Chris Roper said.
Hi mmotte.
The script works for me in Great Cow BASIC (0.94 2015-08-05)
I'm pretty sure that gcbasic already reserves dim+1 positions.
But i see nothing in the zero position of the array, always 0... if the length of the array should be in array(0), then something is wrong here.