SRL Archive - Research & Development Lounge

Setting Array Length and Adding to it (Opinions plz)


Narcle

2008-09-28 20:55:03

Ok I know what these do, but apparently Nava2 doesn't. So here is my take on them:

Length = It is the "whole number length" which starts at 1 and up.
GetArrayLength = Same as Length?

High = the high point of the array, if the array was only [0] it would return 0. So High starts at 0 and counts up from there.

SetArrayLength = Sets the length of the array from 0 or 1? This is the main point of discussion.


Nava2's way of adding to a array:


L := Length(Array);
SetArrayLength(Array, L + 1);
Array[L] := 'ock';


My way:

SetArrayLength(Array, High(Array) + 1);
Array[High(Array)] := 'ock';


Now whose way works, whose is flawed, whats the problems with how its done?

Nava2

2008-09-28 20:56:35

Answer! :d

bullzeye95

2008-09-28 20:58:49

Yours is wrong. If you want to set the length of an array to one more then it is already, then obviously you want one more then the current length of the array (Length(arr) + 1). Here's an example: say you have an array that's a length of 5, or 0..4. If you want to set the length to 6, or 0..5, you'd need to put SetLength(Arr, 6). Length(Arr) + 1 is 6. High(Arr) + 1 is 5.

Sorry if that's hard to understand, but nava2's way is correct.

EDIT: To be more clear, it's like widget said: you need to either set the length to high() + 2, or length() + 1.

Widget

2008-09-28 20:59:50

They both do exactly the same thing :)

Nava2's might be slightly faster since it doesn't call Length/High twice

Edit: Oh, yours need to say High(blar)+2 instead of +1, then they both do the same thing :p

Narcle

2008-09-28 21:07:28

What was getting me was the Array[L] := part. Which is weird when you think about it that your making the Array length L + 1 then say L := but because SetArrayLength starts at 1 then it works.

SetArrayLength(Array, Length(Array) + 1);
Array[High(Array)] := 'ock';

That version of mine would work. And Widget it would only be like 2-3 MS faster.

Which is why I wanted to mainly know which SetArrayLength started at.

Twisted

2008-09-28 22:00:45

still faster though eh? lol, no jk. But why can't you just put a number down instead of doing that?

Santa_Clause

2008-09-28 23:14:34

What do you mean by 'where does SetArrayLength' start from? If you do SetArrayLength(6), it'll make an array from 0..5.

Narcle

2008-09-28 23:25:06

From: Santa_Clause
What do you mean by 'where does SetArrayLength' start from? If you do SetArrayLength(6), it'll make an array from 0..5.


Already been answered…

And I was asking if Set 5 was 0..5 or if Set 6 was 0..5

Wizzup?

2008-09-29 10:34:26

SetArrayLength = SetLength
GetArrayLength = Length;

Low() = Lowest INDEX
High() = Highest INDEX
Length = High() - Low() + 1;

EDIT:

program New;

var
a: array [4..8] of integer;
begin
writeln('low: ' + inttostr(low(a)));
writeln('high: ' + inttostr(high(a)));
writeln('length: ' + inttostr(length(a)));
end.

Successfully compiled (29 ms)
low: 4
high: 8
length: 5
Successfully executed


@ Narcle, your way won't work.

SetArrayLength(Array, High(Array) + 1);
Array[High(Array)] := 'ock';

Should be:
SetArrayLength(Array, Length(Array) + 1);
Array[High(Array)] := 'ock';

Also, if you really want to use arrays efficiently, just use an integer to keep track of it's length. Saves you alot of High() and Length().
Also please note: High is faster than Length.