Hi
My simple question is if the array is defined as myarray(9), can the first element 0 be used ? The help seems a bit unclear in this area. I want to use a loop to cycle through an array to store values and read values from it and need to know if I can start at 0. Currently I am starting at 1 and it works fine, but it just seems like a waste of memory.
My second question is is there a very simple way to clear an array ?
I have the latest version on GC.
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Any help to clarify the Help would be great. If we clarify then please help improve the Help.
You can use element 0. Help tries to clarify that element 0 does hold the length of the array but only when less than 255 elements.... hence, as a general rule do not use element 0 as the array length.
To clear the array. Write a loop that sets the array elements to 0.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To Quote Anobium: "You can use element 0. Help tries to clarify that element 0 does hold the length of the array but only when less than 255 elements.... hence, as a general rule do not use element 0 as the array length."
Now I am confused too :)
A) You Can use Element 0.
B) Element 0 holds the length of the array.
C) As a general rule do not use element 0 as the array length.
So how do we not use element 0 as the length of the array?
Should we define all arrays to be greater than 255 elements to meet criteria C?
Should it read you MAY NOT use Element 0 UNLESS your array is greater than 255 elements?
What happened to the #Option Radix statement we discussed a while ago, specifically to avoid future confusion like this?
Cheers
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When using the method show above element 0 of the array TestVar will be set to the number of items in the array, 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 up to 48 data elements.
Array Length 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 when the array is set as shown above.
The correct method is to use a constant to set the array size and use the constant within your code to obtain the array length.
#Define ArraySizeConstant 500
Dim TestVar( ArraySizeConstant )
SerPrint ArraySizeConstant 'or, other usage
A) You Can use Element 0? To my knowledge - yes you can. Hugh will correct me if I am incorrect.
B) Does element 0 holds the length of the array. No. Not a reliable method. As Great Cow BASIC limits the array size of any array to 10,000 elements this means the array length is handled as a word and therefore cannot fit into element 0 (a byte).
C) As a general rule do not use element 0 as the array length. Recommend you do not. A good method is shown above.
D) So how do we not use element 0 as the length of the array? Use the method shown above - the constant is useful as this can be used to manage loops etc.
E) Should we define all arrays to be greater than 255 elements to meet criteria C? No. As shown in the help the limit on array size varies dependent on the chip type.
The 12F/16F series of chips the array limit is the physical RAM less a few bytes for array handling.
For the Atmel AVR or an 18F there is not limit other than free RAM.
However, Great Cow BASIC limits the array size of any array to 10,000 elements.
D) #Option Radix is on the list of changes.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To clarify, you can use element 0. If you define myarray(9), you end up with an array of 10 elements (numbered 0 to 9). The array starts at element 0 and ends at element whatever you've used in the Dim statement. Dim myarray(9) gives you a 10 element array. This is the same behaviour as you'll see in FreeBASIC.
Element 0 will sometimes hold the array length, but don't rely on it. If you've set multiple elements of the array in a single line as Anobium posted above, element 0 will hold the length. Strings in GCBASIC are just arrays that are treated a bit differently, if you have
Dim MyData As String
MyData = "Hello"
Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it.
Maximum size on 14 bit core PICs (12F/16F) depends on the bank size. The RAM is divided into banks, each consisting of up to 80 bytes depending on the chip. GCBASIC can't split an array over multiple banks, so each array must fit inside of an 80 byte bank. On enhanced midrange PIC (12F1xxx/16F1xxx), PIC 18F and all AVR chips, the RAM is in one continuous block and you can make an array as big as you want provided you don't run out of RAM.
If you need to load a large amount of data into an array and then read but not change it, I'd suggest forgetting about loading it all in one line of code. Use a smaller array if you can, put the data into a data table and load it element by element as you need it. You can't change the elements of a data table (at least not easily) but you can store a lot more data in them than an array will hold. (Data tables can also store types other than byte).
Multi dimensional arrays have been on the to-do list for a while. I've been a little reluctant to implement them because of the multiplication that would be required to access elements - on some chips it would be quite slow and inefficient to do this. However, given that we have hardware multiplication on many chips now, maybe this isn't such a concern now.
Hope that helps!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I disussed this with you briefly Evan. Most basics I've used are dim var(1023)...which to me is var(0) to var(1023). In the GLCD includes GLCDBufferAlias is dimmed 1024 and the array is indexed from 1 to 1024. Evan said "we do it that way". The help shows 1st element as 1. So.....what is correct? What is the 1st element...or the 5th element?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Evan said "we did it that way, and, I will continue to". When we did not support the large arrays that we do today - things were different. The constraints where different also with respect to element(0).
When someone has the time to retro fit all the demos and the Help then this will be cleared up but the libraries will stay as they are using one extra byte.
Evan, I really do appreciate the help you and the other GCB developers provide. I would gladly volunteer but I'm far too green with PICs and GCB. Not to mention my minimal skills with HTML. I'm barely out of the Picaxe womb. That said I'm skilled at drawing schematics and line drawings if you ever need help in that dept.
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi CDRIVE. I've been told to forget Picaxe when using GCB. GBC syntax is quite different and has more features to learn. GCB help is easy to skip over. All info is there if you read it thoroughly. I didn't though, thinking in Picaxe. Maybe my Nextion display isn't working because of what Hugh Considine said above. ie dim nextion as string : nextion = "draw 1,1,100,100,RED" : serprint nextion. Am I sending an extra initial byte?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm confused. Hugh Considine says "Dim MyData As String
MyData = "Hello"
Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it."
I tried the above but seems MyData can be string or array.
This scrolls the press string.--It works.
dim press as string
press = "P R E S S A B U T T O N "
;
main:
GLCDCLS
do Until up_button = 1 or down_button = 1 or left_button = 1 or right_button = 1
GLCDPrint ( 8 , 27 , PRESS )
tmp = Random
press = mid ( press , 2 ) + left ( press , 1, 23 )
loop ;wait for button press
What would press = mid ( press , 1 ) + left ( press , 0, 22 ) do?
Well it compiles but the press string doesn't scroll. It DOESN'T WORK! shock horror probe
So work that one out chaps. It's not really broken so don't fix it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dim MyData As String
"Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it"
Makes it look complicated to me. Seems that a string has a length byte but it's only an array as left ( press , 0, 22 ) don't work. So in an array, dim var(10) is 11 elements and the 1st is the length. Is the length 10 or 11 elements? Sould I try for var=array(1) to array(array(0)-1) 0r to array(array(0)) ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It sets aside 1024 + 1 bytes of ram to store elements 1 - 1024 elements + 1 more byte for element0
Only Only Only, if the Array is written in the manner above does element0 hold the number of elements writtern to the array, Element 0 DOES NOT contain the size of the array,..Only the number of elements written to it and only in the manner above.
When writing an array with this method "Array = data,data,data ,,," The maximum number of bytes that can be written is 250. How do I know this? Because I got a chip and actually tried it. No armchair philosophy or assumptions. 250 falls well witihin the bounds of a byte value.
Who in their right mind would type out more than 250 comma separated byte values on a single line to load into an array? Would you? I think not.
Now consider the following:
Dim TestArray(1024)
Dim Databyte as Byte
Dim ii as word
Databyte = 0
For ii = 1 to 40
TestArray(ii) = DataByte
databyte = Databyte + 2
Next
Does element0 hold the number of bytes written tothe array it? No. 40 byte were written, but the compiler does not decipher the loop code, so it cannot set element0 to 40. The value in element0 will be undetermined.
Is there a system variable that indicates the size of the array. No. But the programmer can set a constant with #define. eg. #define TestArraySize 1024
Now consider.
Dim TestArray(1023)
Dim Databyte as Byte
Dim ii as word
Databyte = 0
For ii = 0 to 1023 '// Changed
TestArray(ii) = DataByte
databyte = Databyte + 2
Next
.
In this code we use element0 just as any other element. Note that I changed the array size to 1023 instead of 1024. Why? Becasue with 1024 there is still that extra byte that we really do not need. With the array sized to 1023, it will actually hold 1024 bytes when element 0 is included.
When in doubt:
1. Read the Help
2. Hook up a chip and actually test
Last edit: William Roth 2017-04-17
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you for the detailed reply. Please under stand I've "emigrated " from a basic that dim val(1024) was from 0 to 1023 ie val(1024) didn;t exist. I am not alone ie "imigrant" to GCB so this point needed clarifying as assuming all basics are the same is not wise. Emphasis in the appropriate help would be hand maybe but then it's up to the reader to take in what's written and not interperet it.
It's little things like in serprint help an example that has serprint portb . So I assumed portb had a value that could be printed,tested. So I used if (portb and 8).>0 in a program but it didn't work. It has been resolved by using pinb instead of portb. Another hangover from another basic which used pinsb.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
BASIC has always been implementation and platform specific so it is any "assumption" that is not wise.
The only Language, other than Java, that was designed with the stated intent of portability was C, and even that has implementation differences, even between C compilers targeting the same platform.
The majority of BASIC dialects use unary based indexing i.e. 1-10 rather than 0-9 as it is easier to grasp for beginners, the target audience for “Beginners All Purpose Symbolic Instruction Code” (BASIC). Also remember that most dialects of BASIC run on top of an operating system so no knowledge of hardware is required. It is only because you are used to working at a low level with bits and bytes that you understand and expected a zero radix.
Think of it this way, when you use a Spreadsheet do you use cells 0 to 99 or 1 to 100?
Cheers
Chris
Last edit: Chris Roper 2017-04-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I haven't used a spread sheet since I did open university in the 80's. That was the last time I used trig to and that was for a game. I write letters in notepad and prefer windows paint to paint shop pro or gimp. Screen co-ordinates start at 0,0 in GCB. Is that the norm? I can't remember what other machine specific did- sinclair,amstrad,commodore,atari st,VB,pascal etc.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
"Option base" is exactly the name of the statement.
'0' means that the first index of all sized arrays begins by 0 (option base 0).
'1' means that the first index of all sized arrays begins for 1.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
My simple question is if the array is defined as myarray(9), can the first element 0 be used ? The help seems a bit unclear in this area. I want to use a loop to cycle through an array to store values and read values from it and need to know if I can start at 0. Currently I am starting at 1 and it works fine, but it just seems like a waste of memory.
My second question is is there a very simple way to clear an array ?
I have the latest version on GC.
Thanks
Any help to clarify the Help would be great. If we clarify then please help improve the Help.
You can use element 0. Help tries to clarify that element 0 does hold the length of the array but only when less than 255 elements.... hence, as a general rule do not use element 0 as the array length.
To clear the array. Write a loop that sets the array elements to 0.
To Quote Anobium: "You can use element 0. Help tries to clarify that element 0 does hold the length of the array but only when less than 255 elements.... hence, as a general rule do not use element 0 as the array length."
Now I am confused too :)
A) You Can use Element 0.
B) Element 0 holds the length of the array.
C) As a general rule do not use element 0 as the array length.
So how do we not use element 0 as the length of the array?
Should we define all arrays to be greater than 255 elements to meet criteria C?
Should it read you MAY NOT use Element 0 UNLESS your array is greater than 255 elements?
What happened to the #Option Radix statement we discussed a while ago, specifically to avoid future confusion like this?
Cheers
Chris
It is possible to set several elements of an array with a single line of code. This short example shows how:
When using the method show above element 0 of the array TestVar will be set to the number of items in the array, 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 up to 48 data elements.
Array Length 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 when the array is set as shown above.
The correct method is to use a constant to set the array size and use the constant within your code to obtain the array length.
A) You Can use Element 0? To my knowledge - yes you can. Hugh will correct me if I am incorrect.
B) Does element 0 holds the length of the array. No. Not a reliable method. As Great Cow BASIC limits the array size of any array to 10,000 elements this means the array length is handled as a word and therefore cannot fit into element 0 (a byte).
C) As a general rule do not use element 0 as the array length. Recommend you do not. A good method is shown above.
D) So how do we not use element 0 as the length of the array? Use the method shown above - the constant is useful as this can be used to manage loops etc.
E) Should we define all arrays to be greater than 255 elements to meet criteria C? No. As shown in the help the limit on array size varies dependent on the chip type.
D) #Option Radix is on the list of changes.
To clarify, you can use element 0. If you define myarray(9), you end up with an array of 10 elements (numbered 0 to 9). The array starts at element 0 and ends at element whatever you've used in the Dim statement. Dim myarray(9) gives you a 10 element array. This is the same behaviour as you'll see in FreeBASIC.
Element 0 will sometimes hold the array length, but don't rely on it. If you've set multiple elements of the array in a single line as Anobium posted above, element 0 will hold the length. Strings in GCBASIC are just arrays that are treated a bit differently, if you have
Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it.
Maximum size on 14 bit core PICs (12F/16F) depends on the bank size. The RAM is divided into banks, each consisting of up to 80 bytes depending on the chip. GCBASIC can't split an array over multiple banks, so each array must fit inside of an 80 byte bank. On enhanced midrange PIC (12F1xxx/16F1xxx), PIC 18F and all AVR chips, the RAM is in one continuous block and you can make an array as big as you want provided you don't run out of RAM.
If you need to load a large amount of data into an array and then read but not change it, I'd suggest forgetting about loading it all in one line of code. Use a smaller array if you can, put the data into a data table and load it element by element as you need it. You can't change the elements of a data table (at least not easily) but you can store a lot more data in them than an array will hold. (Data tables can also store types other than byte).
Multi dimensional arrays have been on the to-do list for a while. I've been a little reluctant to implement them because of the multiplication that would be required to access elements - on some chips it would be quite slow and inefficient to do this. However, given that we have hardware multiplication on many chips now, maybe this isn't such a concern now.
Hope that helps!
Thanks Hugh,
That helps a lot.
Cheers
Chris
Serious ask. Would someone like to update the Help?
I need help on this one.
I disussed this with you briefly Evan. Most basics I've used are dim var(1023)...which to me is var(0) to var(1023). In the GLCD includes GLCDBufferAlias is dimmed 1024 and the array is indexed from 1 to 1024. Evan said "we do it that way". The help shows 1st element as 1. So.....what is correct? What is the 1st element...or the 5th element?
Evan said "we did it that way, and, I will continue to". When we did not support the large arrays that we do today - things were different. The constraints where different also with respect to element(0).
When someone has the time to retro fit all the demos and the Help then this will be cleared up but the libraries will stay as they are using one extra byte.
Please also read this post, https://sourceforge.net/p/gcbasic/discussion/579126/thread/04955d11/#2af2
Anyone willing to update the Help?
Evan, I really do appreciate the help you and the other GCB developers provide. I would gladly volunteer but I'm far too green with PICs and GCB. Not to mention my minimal skills with HTML. I'm barely out of the Picaxe womb. That said I'm skilled at drawing schematics and line drawings if you ever need help in that dept.
Chris
Hi CDRIVE. I've been told to forget Picaxe when using GCB. GBC syntax is quite different and has more features to learn. GCB help is easy to skip over. All info is there if you read it thoroughly. I didn't though, thinking in Picaxe. Maybe my Nextion display isn't working because of what Hugh Considine said above. ie dim nextion as string : nextion = "draw 1,1,100,100,RED" : serprint nextion. Am I sending an extra initial byte?
I'm confused. Hugh Considine says "Dim MyData As String
MyData = "Hello"
Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it."
I tried the above but seems MyData can be string or array.
This scrolls the press string.--It works.
What would press = mid ( press , 1 ) + left ( press , 0, 22 ) do?
Well it compiles but the press string doesn't scroll. It DOESN'T WORK! shock horror probe
So work that one out chaps. It's not really broken so don't fix it.
Is there a question here ?
Dim MyData As String
"Then you can treat MyData as an array. If you access MyData(0), that will give you the length, MyData(1) will contain the first character, and so on. If you've set an array any other way, element 0 won't hold the length. You can do whatever you want with it"
Makes it look complicated to me. Seems that a string has a length byte but it's only an array as left ( press , 0, 22 ) don't work. So in an array, dim var(10) is 11 elements and the 1st is the length. Is the length 10 or 11 elements? Sould I try for var=array(1) to array(array(0)-1) 0r to array(array(0)) ?
"If you access MyData(0), that will give you the length"
Dim val(1024)
How does 1024 fit in one byte?
The array addressing routines stores the array length in an appropriate memory location .
The Help is cLear about setting the length.
I do not think there is a public method to query the array length. I have added to the list of future changes.
Stan,
Consider the following statements.
What does the compiler do?
Now consider the following:
Next
Does element0 hold the number of bytes written tothe array it? No. 40 byte were written, but the compiler does not decipher the loop code, so it cannot set element0 to 40. The value in element0 will be undetermined.
Is there a system variable that indicates the size of the array. No. But the programmer can set a constant with #define. eg. #define TestArraySize 1024
Now consider.
.
In this code we use element0 just as any other element. Note that I changed the array size to 1023 instead of 1024. Why? Becasue with 1024 there is still that extra byte that we really do not need. With the array sized to 1023, it will actually hold 1024 bytes when element 0 is included.
When in doubt:
1. Read the Help
2. Hook up a chip and actually test
Last edit: William Roth 2017-04-17
Thank you for the detailed reply. Please under stand I've "emigrated " from a basic that dim val(1024) was from 0 to 1023 ie val(1024) didn;t exist. I am not alone ie "imigrant" to GCB so this point needed clarifying as assuming all basics are the same is not wise. Emphasis in the appropriate help would be hand maybe but then it's up to the reader to take in what's written and not interperet it.
It's little things like in serprint help an example that has serprint portb . So I assumed portb had a value that could be printed,tested. So I used if (portb and 8).>0 in a program but it didn't work. It has been resolved by using pinb instead of portb. Another hangover from another basic which used pinsb.
"assuming all basics are the same is not wise"
BASIC has always been implementation and platform specific so it is any "assumption" that is not wise.
The only Language, other than Java, that was designed with the stated intent of portability was C, and even that has implementation differences, even between C compilers targeting the same platform.
The majority of BASIC dialects use unary based indexing i.e. 1-10 rather than 0-9 as it is easier to grasp for beginners, the target audience for “Beginners All Purpose Symbolic Instruction Code” (BASIC). Also remember that most dialects of BASIC run on top of an operating system so no knowledge of hardware is required. It is only because you are used to working at a low level with bits and bytes that you understand and expected a zero radix.
Think of it this way, when you use a Spreadsheet do you use cells 0 to 99 or 1 to 100?
Cheers
Chris
Last edit: Chris Roper 2017-04-18
I haven't used a spread sheet since I did open university in the 80's. That was the last time I used trig to and that was for a game. I write letters in notepad and prefer windows paint to paint shop pro or gimp. Screen co-ordinates start at 0,0 in GCB. Is that the norm? I can't remember what other machine specific did- sinclair,amstrad,commodore,atari st,VB,pascal etc.
In visual basic there is the statement "option basic" 0 or 1.
Or when you dimension an array "dim NameVar (1 (2,3,x) to N) as String, Integer ecc...
Not to be overly picky (probably a typo) but in VB that would be "Option Base".
Chris
"Option base" is exactly the name of the statement.
'0' means that the first index of all sized arrays begins by 0 (option base 0).
'1' means that the first index of all sized arrays begins for 1.
I had already added to the list of potential changes. Added last November at the request of Chris Roper. Directive is noted at #option radix
Evan, thanks for that. It'll be useful for those of us that have become accustomed to Option Base = 0.
Is the default going to remain "1"?
Cheers,
Chris