Question about arrays

BlitzMax Forums/BlitzMax Beginners Area/Question about arrays

Arabia(Posted 2010) [#1]
Why does this code give me an attempt to index array element beyond array length at line 3?



Yet, if I comment out line 3 I do not get the same error at line 6?



Am I missing something?


Vampyre(Posted 2010) [#2]
I just had a look at it, and it's effectively calling an error.

But if I initialize the array first with empties, it works

SuperStrict
Local y:String[5]
y = ["", "", "", "", "", ""]
y[5]="z"
y = ["a","b","c","d","e","f"]
Print y[5]
y[5]="z"
Print y[5]



Arabia(Posted 2010) [#3]
Yeah, that's what I worked out. Is this an error with BMax?

An array of [5] should have 6 element 0, 1, 2, 3, 4 & 5

I did a quick search in the forums, but didn't find anything about this. Surely if this is an error within BMax then someone else has come across it and reported it.

It's easy enough to get around, just add one more to the array length. But it really does make the code harder to read for others in my opinion. Unless you are going to initialize all the arrays at the time of declaring them.

Last edited 2010


Thareh(Posted 2010) [#4]
An Array of [5] shouldn't have 6 elements, it should have 5 just as you specified with [5].
So you simply call y[4]="z" to set the last element of that array, 5 will throw an error ^^
The reason for this is that the first element is [0], not [1].

Last edited 2010


Arabia(Posted 2010) [#5]
So, if you initialize an array as:



It automatically increases the array size for you by the looks of it.

I understand what is going on now, thanks Thareh.

I would really expect the above code to throw an error though, especially in SuperStrict.


Arabia(Posted 2010) [#6]
Maybe it's just my brain, but I really hate that array's start at zero.

The first basic I ever used arrays in (GW Basic), allowed you to create an array [10] which actually had 11 elements. I just find it easier to not work with zero - surely I'm not the only one?

Visual Basic had a command (can't remember it's name right now) but which basically allowed you to turn on array numbering - i.e. an array[10] had elements 1 to 10 and not 0 to 9.

This would be a nice option in BM (maybe I'm the only one who would like it)


CS_TBL(Posted 2010) [#7]
(maybe I'm the only one who would like it)

Yes.

With GW-BASIC you're referring to a language which is older than my car, which is now so old I'll swap it for a more recent car this week.

Besides, with the 'until' command it's even easier.

size=666
local b[size]
for t=0 until size
b[t]=7
next

See? One number fits all, it's the number representing the amount of elements, no messing with +1 or -1. It's one number, it's a human number.. it's ... ohwell, you know.. :P


Oddball(Posted 2010) [#8]
y is only an array reference, and not the actual array. Using [ and ] to initialise or fill an array will create a new array and change the y reference to point to it. The old array still exists until it is fully dereferenced. In your code you are simply creating a new array with a different size and reference this new array with y. This is extremely important to note if you are reference the same array from multiple parts of your app, as using [ and ] to populate the array will not affect any of the other references to the original array.


_Skully(Posted 2010) [#9]
0 based arrays are industry standard now, its one of those "Don't touch this" programming paradigms that makes going from language to language easier


GfK(Posted 2010) [#10]
If Mark ever decides to make Blitzmax arrays 1-based, I will personally hunt him down and put scorpions in his sock drawer.


Czar Flavius(Posted 2010) [#11]
When creating an array that you also give the starting values for, don't put a number in the square brackets, leave it blank as String[] = [...]. Blitz will find the size for you.


Arabia(Posted 2010) [#12]
Okay, thanks everyone. Points taken about the zero starting point in arrays.


ima747(Posted 2010) [#13]
I despised the 0 base of things when I first started programming, but I learned to deal with it, and now I can't imagine it any other way. Besides the obvious structural reasons (it's just faster...) there are a lot of number tricks that can be applied in various circumstances that also make other code faster. 0 is a very powerful number since it can be so many things in code, and when you can play with the "meaning" without having to change the value you get some good tricks. as an example 0 is also false so
if(a_number)
'the number is something OTHER than 0, so it can't be the first thing in the array...
end if

can be handy since you can perform a boolean test on non-boolean variables that you may be using to track other things.

But I think Skully hit the bigest point which is it's standard, when you don't have to re-learn how to count when jumping between languages you're less likely to have your brain explode...

and CS_TBL's tip regarding using until instead of to for loops fills in the gap in the middle of being more instantly understandable 0 base.

Last edited 2010


Czar Flavius(Posted 2010) [#14]
That's a nifty trick, but I avoid it unless I am using the variable as an actual boolean. I think a_number <> 0 gives a clearer indictation of what the test means.


Arabia(Posted 2010) [#15]
I really should learn to deal with it. I'm not that against using it, I think I just got into the practice early on and it's hard to make my brain think the zero = first item way :)


Hotshot2005(Posted 2010) [#16]
Arrays are brilliant if you know how to used them and it save you lots of hassle :)

Then there is Type which is even better :)


Czar Flavius(Posted 2010) [#17]
Or an array of types! Heaven.


Arabia(Posted 2010) [#18]
Is there a decent tutorial or topic on using types in arrays?

I've had a quick search in here, but didn't turn anything up.

From what I can see, Types in BM are a bit different to what they are in say Pascal or VB.


dmaz(Posted 2010) [#19]
...and don't forget that when you don't need an index, eachin works great with arrays. and you can use slice syntax to start anywhere in the array.
"for local s:string = eachin (y)[2]" which has come in pretty for me.


Czar Flavius(Posted 2010) [#20]
That is not slicing?


_Skully(Posted 2010) [#21]
Is there a decent tutorial or topic on using types in arrays?

Yes...


Type This
   Field X#,y#
End Type

local TR:This[5]
For local ct:int=0 to 4
   TR[ct]=New This
Next

TR[0].X=4.3
TR[0].Y=3.4


Last edited 2010


Arabia(Posted 2010) [#22]
Thanks _Skully, I'm sure I've seen this somewhere on here but I could find it :)


dmaz(Posted 2010) [#23]
That is not slicing?
Why do you think it isn't? in any case I said that to emphasize the parens around the array object.

Last edited 2010


Czar Flavius(Posted 2010) [#24]
Because slicing involves the ".." operator.


dmaz(Posted 2010) [#25]
"slicing" is just terminology for referencing or extracting parts of an array as many language can have more complex operations. for instance in perl I'm not limited to only ranges using "..". one can also use comma's and negative offsets. so the term is certainly not defined by just that one operator. in any case I have too many languages on my mind as I messed that example up. I get an illegal EachIn while I swear I've done that syntax before in bmx which I thought allowed traversing the array by offset without a copy.


Czar Flavius(Posted 2010) [#26]
Could you show what you did in executable code? I don't think I understood it properly.