Discussion:
How to count elements in a dynamic array?
(too old to reply)
-=[MJ]=-
2004-01-10 03:09:09 UTC
Permalink
Hello peers,

I have a simple function that takes a dynamic array as it's sole argument.
Is there a simple way to return the number of elements in an array within
Delphi/Object Pascal?
Maybe it's just too late at night, but i cannot find anything obvious in the
Delphi for .NET Documentation.

Thank you in advance,

-=[MJ]=-
"Feeding the herd since 1977"
Jamie
2004-01-10 09:37:23 UTC
Permalink
if your using the system level dynamic array then you should be able
to use the Length(Your Array of SomeType) this will give you the number
of elliments that have been allocated in the array..
if you are using memory type arrays that you are creating your self
then you may have a little problem there.
Post by -=[MJ]=-
Hello peers,
I have a simple function that takes a dynamic array as it's sole argument.
Is there a simple way to return the number of elements in an array within
Delphi/Object Pascal?
Maybe it's just too late at night, but i cannot find anything obvious in the
Delphi for .NET Documentation.
Thank you in advance,
-=[MJ]=-
"Feeding the herd since 1977"
-=[MJ]=-
2004-01-08 15:32:06 UTC
Permalink
Post by Jamie
if your using the system level dynamic array then you should be able
to use the Length(Your Array of SomeType) this will give you the number
of elliments that have been allocated in the array..
the code can be simplified as follows:-

procedure quickTest(Oracle: array of string);
var
aCounter: integer;
begin
for aCounter:= 0 to Length(Oracle) do
begin
{ some code using Oracle[aCounter] }
end;
end;

however, using Delphi 8 for .NET the Length function takes a string as an
argument and returns the Length of the string in element [0] and not the
total number of elements passed in the array. This (obviously) leads to 'out
of bounds' type messages when executed.

thank you in advance,

-=[MJ]=-
"Feeding the herd since 1977"
Maarten Wiltink
2004-01-10 16:27:15 UTC
Permalink
Post by -=[MJ]=-
Post by Jamie
if your using the system level dynamic array then you should be able
to use the Length(Your Array of SomeType) this will give you the number
of elliments that have been allocated in the array..
the code can be simplified as follows:-
procedure quickTest(Oracle: array of string);
That's not a dynamic array but an open array. Similar, but neither the
same nor actually exchangeable in older Delphi versions.
Post by -=[MJ]=-
var
aCounter: integer;
begin
for aCounter:= 0 to Length(Oracle) do
That's either 1..N, or 0..Pred(N). In thise case, it's the latter.

Just wondering: do Low() and High() work on dynamic arrays?
Post by -=[MJ]=-
begin
{ some code using Oracle[aCounter] }
end;
end;
however, using Delphi 8 for .NET the Length function takes a string as an
argument and returns the Length of the string in element [0] and not the
total number of elements passed in the array. This (obviously) leads to 'out
of bounds' type messages when executed.
No, that comes from looping up to and including Oracle[Length(Oracle)].
Otherwise, the above code works fine.

Length is by now heavily overloaded and works on short strings, long
strings,
and dynamic (and probably open) arrays. The behaviour you describe is that
for short strings. The behaviour for the other datatypes is different and, I
might add, correct.

Groetjes,
Maarten Wiltink
-=[MJ]=-
2004-01-10 17:55:02 UTC
Permalink
Oh dear. So simple and yet so far. I blame my tiredness for lack of
debugging. ;)

A quick change to

{ for aCounter:= 0 to (Length(Oracle)-1) do }

solves this particular issue.

Thank you both for your assistance, now i just have to read up on open and
dynamic arrays so i can work out what everyone else is talking about.

-=[MJ]=-
"Feeding the herd since 1977"
Nicholas Sherlock
2004-01-15 09:31:46 UTC
Permalink
Post by -=[MJ]=-
Oh dear. So simple and yet so far. I blame my tiredness for lack of
debugging. ;)
A quick change to
{ for aCounter:= 0 to (Length(Oracle)-1) do }
solves this particular issue.
Thank you both for your assistance, now i just have to read up on
open and dynamic arrays so i can work out what everyone else is
talking about.
Much better idea is high(oracle). Makes more sense when reading the code.

Cheers,
Nicholas Sherlock

Continue reading on narkive:
Loading...