Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Array in MMBasic
Author | Message | ||||
RobW Newbie Joined: 13/05/2014 Location: NetherlandsPosts: 5 |
Maybe a stupid question but programming has been a long time ago I have a TFT Maximite and use it in a (race) car to keep track of the engine. In the main program its shows a dash and I store revolutions, water and oil temp and oil pressure to a file. I made a sub routine to show the data in a line chart. I open the file and display the values but I would like to store the values in an array to scroll, zoom etc. Now I have this: dim dt(4) as integer input #2,dt(1),dt(2),dt(3),dt(4) To have an array it should be something like: dim dt(count,4) as integer But it won't work... (Error: Invalid array dimension) What am I doing wrong? |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6102 |
Your first example Dim dt(4) creates an array of one dimension. In the second example "dim dt(count,4) as integer" you are trying to create an array with two dimensions of 'count' by 4 You have not given the variable 'count' a value so it is zero. This is what gives an error. The rest of the line "as integer", is not a MMBasic option. The array will be either floats dt()or strings dt$() Unless the version of MMBasic for the TFT does things differently Jim VK7JH MMedit MMBasic Help |
||||
RobW Newbie Joined: 13/05/2014 Location: NetherlandsPosts: 5 |
Now I want to have a dynamic array: count =1 dim dt(count,4) input #2,dt(count,1),dt(count,2),dt(count,3),dt(count,4) count=count+1 This won't work because when count = 2 the array (out of bounds) does not resize. What am I doing wrong? |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1873 |
RobW When you dimention the array you can't use count as a dimention If the maximum count is going to be 100 then you use Dim dt(100,4) Paul "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
RobW Newbie Joined: 13/05/2014 Location: NetherlandsPosts: 5 |
But I do not know the lenght of the file at this point. Is it not possible to redim the array? |
||||
palcal Guru Joined: 12/10/2011 Location: AustraliaPosts: 1873 |
It doesn't matter if the dimension is larger. eg if the max count is unknown use a number that count wont exceed. Paul "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Dimensioning an array is to commit a block of memory for the purpose. Once you DIM an array I believe it causes an error if you try do DIM the same array a second time. Memory is a limited thing. It is used by arrays, variables, programs etc.. Once you use it all then things crash. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6102 |
You cannot REDIM an array and if you want to change the size you have to ERASE the array first and that will loose the contents. You can read the data in first to count the number of rows needed (if the data is not being streamed) or have two arrays and when the first gets full, create the second one with a reasonable number of extra rows and then copy the old data from the first array into the second array. Both methods can be rather slow. Read, process on the fly and save to a second 'log' file is another choice. Making the array big enough in the first instance is idea if memory permits. Jim VK7JH MMedit MMBasic Help |
||||
RobW Newbie Joined: 13/05/2014 Location: NetherlandsPosts: 5 |
I tried following: dim dt(13000,4)(13k is max I tried) which is OK for 20 minutes logfile so this will do at the time I will limit the logfile by logging less values. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6102 |
With big arrays it might be worth using the zero cell in the array. The manual does a better job of explaining it than I could. The Maximite has very limited memory once you start playing with big arrays. In brief: DIM dt(4) actually sets up 5 cells 0 to 4 and the first cell is ignored if you have the OPTION BASE set to 1. setting OPTION BASE to 0 and using 0 to 3 as your indexes will save 13k*4 bytes in your case. Either way, I think you will have to reduce the size of your array considerably. If your data can be formatted as fixed length records in the file, you can uses random access to get to the required time-slot for zooming. That way most of the data is kept on disk. Read up on Random Files (Appendix I) Jim VK7JH MMedit MMBasic Help |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3816 |
At 4 bytes each that's about 13K * 4 * 4 = 208K which doesn't fit in the RAM so I'm puzzled why no error. John |
||||
RobW Newbie Joined: 13/05/2014 Location: NetherlandsPosts: 5 |
I just tested it in MMEdit with MMBasic.exe in Windows, no errors. But I think the Random files are the way to go, as I said it has been a long tme ago so I never thought of this..... Thanks! |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3816 |
Well, it may work on Windows but I reckon it won't on a 'mite! John |
||||
hitsware Guru Joined: 23/11/2012 Location: United StatesPosts: 535 |
> MMBasic.exe in Windows I would not use that as any kind of standard . I haven't really pursued it , but get the impression ( compared to the DuinoMite or MaxiMite version ) that that version is pretty ' not finalized ' ( for lack of a better term ) ........ |
||||
Print this page |