SRL Archive - Help and Support

Quick "Length" question


Narcle

2008-02-08 08:12:41

How exactly does GetArrayLength count it?

For example:
Say A is the Array
A := [2342,432342,234234,234323];

If you get the array length like:

GetArrayLength(A) = 4 or 3?
Does it equal 4 or 3?

A := [32342];
Would this be a Array length of 0 or 1?

Just need this clarified for me. :D

Method

2008-02-08 09:11:20

It counts the number of entries in the array. The first one would return 4, the second one would return 1.

Sumilion

2008-02-08 10:07:44

It's easy to test it out …

program New;
var
A: TIntegerArray;
begin
A := [2342,432342,234234,234323];
Writeln(IntToStr(GetArrayLength(A)));
end.

Anyway, Length, or GetArrayLength (they do the same) gets the number of values stored in the array. High(); gets the last index number. So for example A[High(a)] = 234323.

Length(a) = High(a) + 1

Narcle

2008-02-08 10:31:31

Okay so for like a script that goes through the array looking at each one example:

A := [23423,423523,5436,34563];

For i := 0 to High(A) do
FindColor(A, etc…);
Would work correctly then?

If i := 0 to 0 (for first find) it still looks at first integer in array correct?
Its just weird seeing it as 0 to 0 (the concept anyways).

Oh and so
GetArrayLength-1 = High(A) ?
(Yes algebra ftw)

Only asking this question because I recently used it in the script. I did do it right as I just found out from you guys. Seemed to be messing up when it ran for some people tho. (not going through the full search… like it wasn't look at the first array integer)

The Claw

2008-02-09 09:46:49

who caer s man…..its all relavitn anyway, time and hshit,,,,,,,,

Wanted

2008-02-09 11:55:03

for I := 0 to (GetArrayLength(OB) - 1) do

GetArrayLength or Length returns the numerical value such as ['a', 'b'] length would be two but if you wanted to sort through it you would use:
for I := 0 to (GetArrayLength(WhatEver) - 1)

Santa_Clause

2008-02-09 16:46:02

From: IceFire908
for I := 0 to (GetArrayLength(OB) - 1) do

GetArrayLength or Length returns the numerical value such as ['a', 'b'] length would be two but if you wanted to sort through it you would use:
for I := 0 to (GetArrayLength(WhatEver) - 1)


High(Array) is faster than (GetArrayLength(Array) - 1).

Wanted

2008-02-09 18:06:20

From: Santa_Clause
High(Array) is faster than (GetArrayLength(Array) - 1).


var
TSA: TStringArray;
GAL, H, I: LongInt;

begin
ClearDeBug;
TSA := ['A', 'B', 'C'];
GAL := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('GetArrayLength Avg. : ' + FloatToStr((GetSystemTime - GAL) / 1000000) + ' ms.');
H := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('High Avg. : ' + FloatToStr((GetSystemTime - H) / 1000000) + ' ms.');
end.

They are both exactly the same.

If I were to guess, the source for High looks exactly this: Result := GetArrayLength(Array) - 1;

Bobarkinator

2008-02-09 18:14:01

From: IceFire908
var
TSA: TStringArray;
GAL, H, I: LongInt;

begin
ClearDeBug;
TSA := ['A', 'B', 'C'];
GAL := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('GetArrayLength Avg. : ' + FloatToStr((GetSystemTime - GAL) / 1000000) + ' ms.');
H := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('High Avg. : ' + FloatToStr((GetSystemTime - H) / 1000000) + ' ms.');
end.

They are both exactly the same.

If I were to guess, the source for High looks exactly this: Result := GetArrayLength(Array) - 1;


Hmm, lets take a look at the code


var
TSA: TStringArray;
GAL, H, I: LongInt;

begin
ClearDeBug;
TSA := ['A', 'B', 'C'];
GAL := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('GetArrayLength Avg. : ' + FloatToStr((GetSystemTime - GAL) / 1000000) + ' ms.');
H := GetSystemTime;
for I := 0 to 1000000 do
IntToStr(High(TSA));
WriteLn('High Avg. : ' + FloatToStr((GetSystemTime - H) / 1000000) + ' ms.');
end.


Plus why are you doing the IntToStr?

Wizzup?

2008-02-09 18:51:47

From: Sumilion
It's easy to test it out …

Length(a) = High(a) + 1


program New;

var
myar: array [2..4] of integer;
begin
writeln(inttostr(length(myar)));
writeln(inttostr(high(myar)));
writeln(inttostr(low(myar)));
end.

Choose your tests wisely, my friend. ;)

High is not just Length - 1…
High is the value of the maximum index of an array, low is the value of the minimun index of an array.
Hence the names. :p

I think you can state that:
Length() = High() - Low() + 1;

mastaraymond

2008-02-10 05:55:46

From: Wizzup?
program New;

var
myar: array [2..4] of integer;
begin
writeln(inttostr(length(myar)));
writeln(inttostr(high(myar)));
writeln(inttostr(low(myar)));
end.

Choose your tests wisely, my friend. ;)

High is not just Length - 1…
High is the value of the maximum index of an array, low is the value of the minimun index of an array.
Hence the names. :p

I think you can state that:
Length() = High() - Low() + 1;

To be honest, we hardly use static array's. And If we do, we wouldn't need the low and high, since you can just remember it :rolleyes:.

Dynamic arrayzzzz ftw! <3

Wizzup?

2008-02-10 08:30:00

From: mastaraymond
To be honest, we hardly use static array's. And If we do, we wouldn't need the low and high, since you can just remember it :rolleyes:.

Dynamic arrayzzzz ftw! <3


I was only explaining that length <> high + 1.

n3ss3s

2008-02-10 09:07:28

Length() = High() - Low() + 1;


Correct, because High([]) returns -1, and Low([]) returns 0, so -1 + 0 is 0 and the lenght of [] is 0 :)


program New;
Var
TIA: TIntegerArray;
begin
TIA[High(TIA)] := 0;
end.

gives an out of range exception, because our arrays declared later than the var defining go from 0->…