Array
BlitzMax Forums/BlitzMax Beginners Area/Array
| ||
I like to make an array of a user type. This is very common in c c++, For example: type x field f1:int field f2:float end type then suppose this is correct: Local MyArray:x[] MyArray = New x[100] MyArray[0].f1 = 1204 MyArray[0].f2= 25.45 I tried something like that but it didn't work. Any idea to do such a thing? Also, is any Array library available? I don't want to make one if there is one for BlitzMax. Also, Blitz's site forum search doesn't work, any comment? |
| ||
Type x Field f1:Int Field f2:Float Field name:String End Type Local myarray:x[] myarray = myarray[..100] For c = 0 To 3 myarray[c] = New x RestoreData mydata ReadData myarray[c].name,myarray[c].f1,myarray[c].f2 Next Print myarray[2].name #mydata DefData "John",100,29.4 DefData "Larry",4444, 12345.45 DefData "Fred",3,3.19 |
| ||
Beautiful, thanks. |
| ||
A textbook style example: Local c=6 Local r=6 Type x Field a:Int=0 Field b:String="" End Type Local map:x[c,r] map[0,0]=New x map[1,1]=New x map[2,2]=New x map[3,3]=New x map[0,0].a=23 map[1,1].b="hello" Print map[0,0].a Print map[1,1].b |
| ||
MyArray[0].f1 = 1204 Before you can access the members, you need to create the objects.MyArray[0].f2= 25.45 I tried something like that but it didn't work. Any idea to do such a thing? MyArray[0] = New x MyArray[0].f1 = 1204 etc.. |
| ||
Yes, I just wanted to provide reference for using 2d array and types. map[x,y]=new x above, is creating object, which is both 2d and have type. I thought this is helpful more than the examples in "Beginners guide ..". Actually, map[0,0].a, and map[0,0].b, would show the usage of type and 2d array better. It shows map[0,0] has two cells. If there be any question, I will include that instance in my example. Finally, bradford6's codes above, creates objects in a loop. Which is a smart and beautiful method. But, I find his example for beginners, like me, too advanced. Teaching is about making things easier. |
| ||
Type x Field f1:Int Field f2:Float Field name:String End Type Local myarray:x[] myarray = myarray[..100] myarray[0] = New x myarray[0].name = "Big Bird" myarray[0].f1 = 100 myarray[0].f2 = 29.4444 myarray[1] = New x myarray[1].name = "Bert" myarray[1].f1 = 444 myarray[1].f2 = 3.3333 myarray[2] = New x myarray[2].name = "Ernie" myarray[2].f1 = 55555 myarray[2].f2 = 1.001 Print myarray[2].name Print myarray[0].f1 |
| ||
Evolving all above babies: Strict Type x Field a:Int=0 Field b:String="" Method Print_BlitzMax() For Local i=0 To 7 Print "BlitzMax" Next End Method End Type Type world_map Field c=6 Field r=6 Field map:x[c,r] End Type Local myworld:world_map =New world_map myworld.map[0,0]=New x myworld. map[1,1]=New x myworld. map[2,2]=New x myworld. map[3,3]=New x myworld.map[0,0].a=23 myworld.map[0,0].b="hello" Print myworld.map[0,0].a Print myworld. map[0,0].b myworld.map[0,0].Print_BlitzMax() |
| ||
Medi, I like that idea of making things simpler. Check out my new cool signature for how to put code in boxes. |
| ||
Check out my new cool signature for how to put code in boxes. You should use either the "faq" forum code, or a relative path, so that it'll use the domain the user is using, which might not be blitzmax.com. |
| ||
OK, now I see, I was wondering how to make my codes appear the ways your does. Now, I know. Thanks, I will follow Augen's guide line. |
| ||
Augen, if I put my codes within ...does it appear the way FlamDuck's codes has appeared? Is that what your sheet says? The reason I want to make sure is that I don't want to mess up forum pages by practices. |
| ||
Never mind Augen, I got it. |
| ||
This example, is a simplified form of what I was finally able to use in the map editor that I created for my program. I think it might help beginners like me to organize their mind and their codes - at least to begin with. Here are two classes. In the second one there is a 2d array. Notice the way the array is objectified, and needs necessarly to do so. I learned this from FlameDuck (the code above) and have applied it the way you can see. Unfortuanely, [..100] doesn't work for 2d arrays. So, for dynamic array, we need few more functions that one day I may code it and show it. SuperStrict '////////////////////////// Type report_cells Field a:Int=0 Field b:String="" Method Print_BlitzMax() For Local i:Int=0 To 7 Print "report_content method works" Next End Method End Type '///////////////////////////// Type report_table Field column:Int=6 Field row:Int=6 Field table:report_cells[column,row] End Type '////////////////////////////// Local the_report:report_table=New report_table For Local m:Int=0 To the_report.column-1 For Local n:Int=0 To the_report.row-1 the_report.table[m,n] = New report_cells Next Next For Local m:Int=0 To the_report.column-1 For Local n:Int=0 To the_report.row-1 report_cells(the_report.table[m,n]).a=12 report_cells(the_report.table[m,n]).b="hi there!" Next Next Print report_cells(the_report.table[1,1]).a Print report_cells(the_report.table[0,0]).b Print report_cells(the_report.table[2,3]).b Print report_cells(the_report.table[1,4]).a Print report_cells(the_report.table[3,2]).b Print report_cells(the_report.table[2,3]).b |