Multi-dimension Arrays...

BlitzMax Forums/BlitzMax Programming/Multi-dimension Arrays...

SebHoll(Posted 2007) [#1]
Is there any difference between setting up an array using...
Global myArray:Int[x,y]
... and ...
Global myArray:Int[x][y]
...and if so, what?

Thanks


Seb


Azathoth(Posted 2007) [#2]
The second one is an Array of an Array of Int, and the first one can't use slicing.


SebHoll(Posted 2007) [#3]
Thanks - so basically I should use [x][y] whenever possible to enable slicing. So which one does BlitzMax use for string arrays? I.e. I think I've worked this one out myself ;-).
Global myStringArray:String[x]
E.g. let's say I wanted to get the 4th (pos 3) character from the 2 (pos 1) string, would I do something like...
Print Chr$(myStringArray[1,3])
... or should I do ...
print Chr$(myStringArray[1][3])
Update
I realised it would be quicker to test this myself, and the first attempt threw an "Incorrect number of dimensions error." but the second one worked fine. Therefore I assume a string array is an array of an array.

Regarding the difference between the two, is it that a multi-dimension array basically means you've got fixed dimensions whereas string arrays require an array of an array because each string in the array can have a different length and therefore will require a different 'dimension'?


Brucey(Posted 2007) [#4]
or maybe something like :
myStringArray[1][3..4]

otherwise you get the ascii value...
SuperStrict

Global myStringArray:String[] = ["number one", "number two", "number three"]

Print myStringArray[1]
Print myStringArray[1][3..4]
Print myStringArray[1][3]



SebHoll(Posted 2007) [#5]
Right, I get it now. Complex array work in the past has been kind of "fingers crossed" if it works and if it doesn't tweak it and try again. This should hopefully make life a lot easier.

@ Brucey: I wondered how I could get something like myStringArray[1][3] to return a string as oppose to the ASCII or Unicode value. I've always wrapped it in Chr$() instead but I thought there must be a better way. From what you've written, I assume using slicing will cause an array to be returned which Max will handle and therefore output as a string. Brilliant!

I know this probably wouldn't have worked, but I always thought BlitzMax strings were basically Integer arrays where each element of the array contains the ASCII or unicode value of a letter. So..... Why doesn't this work? BlitzMax's error handler seems to be able to differentiate between the Int Array and a String, but if they are basically the same why should it...
Global myMakeShiftStringArray%[] = [65,66,67,68,69,70] 'ASCII/Unicode Values for "ABCDEF"

Print myMakeShiftStringArray
I know, I know, if only life were that simple... :-)


Brucey(Posted 2007) [#6]
So..... Why doesn't this work

Because BlitzMax arrays of "Ints" are too big. Remember, Ints are 4 bytes. An ascii character is 1...

so..
SuperStrict

Global myMakeShiftStringArray:Byte[] = [65:Byte,66:Byte,67:Byte,68:Byte,69:Byte,70:Byte,0:Byte]

Print String.fromCString(myMakeShiftStringArray)

Works. But it's not something you'd want to use very often, I expect ;-)


Brucey(Posted 2007) [#7]
...and note the 0 byte on the end.. it's probably important.


SebHoll(Posted 2007) [#8]
Good point! But aren't BlitzMax strings Unicode friendly therefore there must be two bytes per character - i.e. a short array? However BlitzMax still won't print the array without converting the array using String.FromWString()

SuperStrict

Global myMakeShiftStringArray:Short[] = [65:Short,66:Short,67:Short,68:Short,69:Short,70:Short,0:Short]

Print String.FromWString(myMakeShiftStringArray)
Therefore it seems as though BlitzMax strings can't be created manually as it were from number arrays without using one of those String functions, which is what I was wondering if you could 'trick' Max into doing. Nevermind... Not something I'd ever use but it was just to satisfy my curiosity.


Otus(Posted 2007) [#9]
String is a class, which probably extends int (or short?) array in a sense. So impossible to trick Max from .bmx code. Using c code, however?