DefData, ReadData... help.

BlitzMax Forums/BlitzMax Beginners Area/DefData, ReadData... help.

Using ReadData how can I read a specific cell?

Example

#setting
DefData 0,2,3,5,6

Let's say I wana read the 5th cell, what would you do.

Basicly Im trying to access #map_main_data get 3rd/4th cell which is 10 to determine how many cells horizontal and then vertical #map_main_layer1 has.

#map_main_data
DefData 32,32,10,10,2

#map_main_layer1

DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0


The easiest way is probably to use a special value to indicate the end of your dataset, and then read the data and put it in an array until it hits your end value... You can then check the specific cell by looking what value that position in the array has.

Data is not effectively read randomly. Tthe best way to do it is to store it in to an array like this:
Strict
Local info%[5]
Local bitmap%[10,10]
For Local i= 0 To info.length-1
	ReadData info[i]
Next
Print info[4]   'this is the fifth element.
For Local down% = 0 To info[3] -1						' accesses the third element in the info array
	For Local across% = 0 To info[2] -1   			' accesses the fourth element in the info array
		ReadData bitmap[across,down]				' stores data in the array.
	Next
Next

#map_main_data
DefData 32,32,10,10,2


#map_main_layer1

DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0


another example is to use types like this:
Strict
Type tinfo
	Field a%
	Field b%
	Field width%
	Field height%
	Field c%
End Type

Local info:tinfo = New tinfo
Local map%[10,10]
RestoreData Map_main_data
ReadData info.a,info.b,info.width,info.height,info.c

Print info.height   'this is the fifth element.
RestoreData Map_main_layer1
For Local down% = 0 To info.height-1 				
	For Local across% = 0 To info.width-1
		ReadData map[across,down]			' stores data in the array.
	Next
Next

#map_main_data
DefData 32,32,10,10,2


#map_main_layer1

DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0


edited: some stuped syntax errors I made corrected. I am surpriced the compiler did not catch them when I ran it.

I like your first example, I'm new to the BMAX syntax I used to use B+. I'm pretty much a nooblet and it took me abit of time to understand it. So basicly your storing ALL the data into two arrays.

I like your first example but it has a syntax error in it could you correct it?

"index array element beyond array length"

That's what labels are for, I thought, it allows you to set the position where you read from. You could scatter labels thoughout your data at periodic places and then jump to the offset you want and seek from there.

Well basicly the maps will be read from external files and map size will differ from maps to maps, I'm not sure how labels work yet but if it is what it sounds to be, it'd be a large hassle to work with those, no?

*pokes jesse for a code fix* ;D

.nonsense removed

uh?

uh?

from wiki : Nonsense is an utterance or written text in what appears to be a human language or other symbolic system, that does not in fact carry any identifiable meaning. Nonsense is generally used by school-age children as a form of communication among their peers.

@Kaisuo i just edited my post because i thought what i wrote was stupid :)

I tried it again and it works fine maybe you didn't copy it correctly

I tried it again this time in debug mode and it tracked the error this time. is a stuped mistake on my part:

when I created array info, I made it 5 elements in size. that number is stored in the "length" part of the array.
But when the program tried to read the data with the for loop, it was reading 0 to 5 which is 6 elements. The true array size is 0 to 4 which is five elements. So the for loop should read like this:
For Local i= 0 To info.length-1

the same go for the other 2 "for" statements
I fixed it in the second example but didn't pay attention on the first one.
I have edited the code and it works fine now.

I guess it only catches the error in debug mode. weird!


Basicly Im trying to access #map_main_data get 3rd/4th cell which is 10 to determine how many cells horizontal and then vertical #map_main_layer1 has.



This is how we used to do it in the old days:

SuperStrict						'SuperStrict mode: all variables' type and scope must be declared

Local rows:Int					
Local cols:Int

RestoreData map_main_layer1		'Move to the start of the "map_main_layer1" data block

ReadData rows					'Read the first data value and store it in "rows" variable
ReadData cols 					'Read the second data value and store it in "cols" variable

Local layer1:Int[rows, cols]	'Declare a two dimensional array to hold the remaining data

								'BlitzMax arrays start at zero so the maximum index value 
								'is always: dimension length - 1
For Local r:Int = 0 To rows - 1	

	Local test:String = ""		'DEBUG PURPOSES: Define a string to store a row
	
	For Local c:Int = 0 To cols - 1 
	
		ReadData layer1[r, c]	'Read in the next data value and store in the 
								'two dimensional array at index r,c
								
		test :+ layer1[r, c]	'DEBUG PURPOSES: Append the cell to the test string
								'"test :+" is shorthand for "test = test +" 
	Next
	
	Print test					'DEBUG PURPOSES: Print the row to the console
	
Next

End

#map_main_layer1				'Data block label
DefData 10, 10					'Number of rows, Number of columns
DefData 1,0,0,0,0,0,0,0,0,2		'Cell data: quantity of cells = (rows x columns)
DefData 0,3,0,0,0,1,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,1,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,8,0,4,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,0,0,0,0,0,0,0,0,0
DefData 0,9,0,0,0,0,0,0,0,4


EDIT: Added some comments to the code.

yes it is basically the same thing. yours is a better method for readability purpose. I was just illustrating the use of arrays.