Author Topic: Difference between ArrayHigh and GetArrayLength  (Read 1520 times)

0 Members and 1 Guest are viewing this topic.

Offline utkesmer

  • Major(1)
  • Posts: 44
Difference between ArrayHigh and GetArrayLength
« on: April 24, 2011, 03:13:04 pm »
What is the difference between ArrayHigh and GetArrayLength?

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Difference between ArrayHigh and GetArrayLength
« Reply #1 on: April 24, 2011, 03:20:47 pm »
one of them didn't work properly on some systems iirc

Offline Polifen

  • Soldier
  • **
  • Posts: 127
Re: Difference between ArrayHigh and GetArrayLength
« Reply #2 on: April 24, 2011, 03:27:23 pm »
GetArrayLenght returns ArrayLenght, the one set with SetarrayLenght on dynamic array.
ArrayHigh returns highest set value.

If you have array with 10 values, but it was set to 20 with SetArrayLength:
-GetArrayHigh will return 20
-ArrayHigh will return 10

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Difference between ArrayHigh and GetArrayLength
« Reply #3 on: April 24, 2011, 03:34:09 pm »
Quote
If you have array with 10 values, but it was set to 20 with SetArrayLength:
-GetArrayHigh will return 20
-ArrayHigh will return 10
If you have an array with 10 values, you can't set it to 20 since it's static.

ArrayHigh returns the index of the highest element
GetArrayLength returns the length of an array (number of elements)

Since index of the first element is always 0 (except crapcore strings), ArrayLength = GetArrayLength - 1
« Last Edit: April 24, 2011, 03:41:34 pm by tk »

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Difference between ArrayHigh and GetArrayLength
« Reply #4 on: April 24, 2011, 07:26:02 pm »
I just want to state that I once (a long time ago) had an issue with ArrayHigh once, and only used GetArrayLength after that without issues (I don't recall exactly the issue, could have been something unrelated. it was a very long time ago).

Offline Polifen

  • Soldier
  • **
  • Posts: 127
Re: Difference between ArrayHigh and GetArrayLength
« Reply #5 on: April 25, 2011, 04:16:33 am »
Quote
If you have array with 10 values, but it was set to 20 with SetArrayLength:
-GetArrayHigh will return 20
-ArrayHigh will return 10
If you have an array with 10 values, you can't set it to 20 since it's static.

ArrayHigh returns the index of the highest element
GetArrayLength returns the length of an array (number of elements)

Since index of the first element is always 0 (except crapcore strings), ArrayLength = GetArrayLength - 1

I mentioned 3 lines higher that GetArrayHigh is mostly dedicated to dynamic arrays.
If you
Code: [Select]
SetArrayLength(my_array, 20)
and then
Code: [Select]
for i := 1 to 10 do my_array[i] := 1;
then GetArrayLength should return 20 and ArrayHigh 10 ( or 9, I can never remember that ).

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Difference between ArrayHigh and GetArrayLength
« Reply #6 on: April 25, 2011, 09:00:39 am »
I mentioned 3 lines higher that GetArrayHigh is mostly dedicated to dynamic arrays.
If you
Code: [Select]
SetArrayLength(my_array, 20)
and then
Code: [Select]
for i := 1 to 10 do my_array[i] := 1;
then GetArrayLength should return 20 and ArrayHigh 10 ( or 9, I can never remember that ).
that doesn't make any sense. All elements of the array have some value initially, the scriptcore doesn't care if you wrote the 0 there or if it was there initially. Arrayhigh should return 20

Edit: both only work on dynamic arrays btw (iirc). Why the hell would you want to get the length of a static array if you know it already because you define it yourself o.o One of the functions was added after the other one was found to generate errors on some systems. Can't recall which one exactly is faulty, so you're welcome to do some research and dig up old posts etc. Or just use the one you like and check if the script works fine :Р I personally used getarraylength, and don't recall that i had any troubles with it. Gotta note that dynamic arrays alone already seemed to make scripts unstable, but that is debatable (others say i'm just paranoid about it)

Edit2: oh yeah, and when i mentioned the scriptcore not knowing about the values in an array etc, i implied that the array elements are not set to some default value as far as i know. So if you create an array, it is a good practice to reset all values to whatever you consider default. Because you could have anything in there depending on scriptcore's mood (checked and confirmed by getting access violations :D). So if you meant that arrayhigh would return the index of the highest assigned element of the array,... there could be pretty much anything, and you could just as well get an error instead of an int that you desired :F
« Last Edit: April 26, 2011, 05:08:47 am by dnmr »