How can I go through all entries in a type?

BlitzMax Forums/BlitzMax Beginners Area/How can I go through all entries in a type?

BachFire(Posted 2007) [#1]
Hey,

I need to manage a big amount of data, and handle it as a database. I thought I could use Type to do that? I remember I did that a few years ago when I used BlitzPlus. I need to go throug all entries in a type, until I find the one I need, so I can extract the data. But I have already hit a wall.. I worked off the Type example found under Modules in MaxIDE. Here it is:

Type TVector
	Field	x,y,z
End Type

Local a:TVector=New TVector

a.x=10
a.y=20
a.z=30

For b=EachIn TVector
 Print b
Next


After all is created, I try to list all in that Type. Apparantly I cannot use EachIn with Types, which seems odd (I guess..). Or am I using it wrong? How can I go through all?

I have looked at the List feature of BMax, but can't quite work out if that can be used to manage databases..

PS. When I list code in these forums, how can I list it so it's smaller and scrollable?? Useful for large code, but don't know how..


TomToad(Posted 2007) [#2]
That's pretty much what lists are for.
Type TVector
    Field x,y,z
End Type

Local VectorList:TList = CreateList()
Local a:TVector = New TVEctor

a.x = 10
a.y = 20
a.z = 30

ListAddLast(VectorList,a)

For Local b:TVector = Eachin VectorList
    Print b
Next

You can also use an Array as well, if you need a more random access than lists provide.
Local Array:TVector[10]
For Local i:int = 0 to 9
    Array[i] = New TVector
    Array[i].x = 10
Next

For Local b:TVector = Eachin Array
    Print b
Next



Brucey(Posted 2007) [#3]
For larger data sets, a TMap (BRL.Map) is better. Thinking in terms of databases, you define each entry with a unique "key", which you use to store/retrieve your type.
Type TVector
    Field x,y,z

  Method key:String()
   return x + "," + y + "," + z
  End Method
End Type

Local map:TMap = new TMap

local a:TVector = new TVector
a.x = 10

map.insert(a.key(), a)

Local b:TVector = TVector(map.ValueForKey("10,0,0"))
Print b.x

Not a great example...

Of course if your data set is very large, why not use a proper database? There are in-memory ones like SQLite that are designed for the task.


Dreamora(Posted 2007) [#4]
Just to mention: you can iterate through TMaps as well, either through the keys or the values.