Simple array help needed..
BlitzMax Forums/BlitzMax Beginners Area/Simple array help needed..
| ||
I've never worked with arrays in BlitzMax or any Blitz language, so I would really appreciate some help here or maybe some sample code? I have a list of first and last names. I want to make 2 seperate arrays to store both kinds so that I can randomly choose one of each to generate a full name ingame. I hope that made sense.. Thanks! |
| ||
Heres some code for ya =) (showing dynamic arrays and array passing to functions)Local firstnames:String[] = [ "bob", "jon" ] Local lastnames:String[] = [ "conner", "walker" ] Function RandomName:String( a:String[], b:String[]) Return a[ Rand( 0, a.Length - 1)] + " " + b[ Rand( 0, b.Length - 1)] EndFunction Print RandomName( firstnames, lastnames) EDIT: sorry about the bug in the code... |
| ||
Thanks! Although could you please help explain the following line more:Return a[ Rand( 0, a.Length - 1)] + " " + b[ Rand( 0, b.Length - 1)]' :D |
| ||
this code is straight from Blitz MaxIDE you can lookit up under arrays.Arrays Arrays are used to store sequences of variables, or 'elements'. An element within an array is accessed by 'indexing' the array with an integer offset. The general syntax for indexing an array is: ArrayExpression [ Index1,Index2 etc... ] Creating Arrays The most common way to create an array is when declaring a variable: Local int_array[10] This will initialize the int_array variable with a 10 element array. You can declare an 'empty' array by using []: Local int_array[] An empty array is identical to an array with 0 elements. Arrays may also be created 'on the fly' using the syntax: New ElementType[Dimension1,Dimension2 etc...] This returns an array of the specified dimension(s) with each element initialized to Null. For example: Local int_array:Int[] int_array=New Int[10] 'Auto arrays' may be created using the syntax: [Element1,Element2 etc...] This returns a 1 dimensional array containing the specified elements, for example: Local int_array[]=[1,2,3,4,5] Each element of an auto array must have exactly the same type. If necessary, you can use type conversions to enforce this. Arrays also provide the following methods: Method Description Sort( ascending=True ) Sort the array. Dimensions:Int[]() Get array dimensions. If the array is empty, an empty array is returned. Here are some examples of using array methods: |
| ||
there's also a page on the blitzwiki that expands on the docs. |
| ||
Thanks guys. @ Perturbatio: I checked Blitzwiki before and couldn't find that page. I guess that was because I searched for "array" not "arrays". |
| ||
Simple example:' Initialise arrays for first and last names Global first$[5] Global last$[5] ' Set name values first$[0]="Dave" first$[1]="Joe" first$[2]="Bob" first$[3]="John" first$[4]="Harry" last$[0]="Smith" last$[1]="Jones" last$[2]="Murray" last$[3]="Oakes" last$[4]="Edwards" ' Randomise seed so we get different random numbers each time SeedRnd(MilliSecs()) ' Print random first name and random last name Print first[Rand(4)]+" "+last[Rand(4)] WaitKey() |
| ||
Wow, that's great Simon :D Thanks again everyone. |