944,043 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Sep 29th, 2006
0

Determining an Array's Length in Pascal

Expand Post »
I am a mainly Java and VB programmer. However, I am writing a program that generates computer code in various languages. One of these is Pascal.

What I am asking is simple.

To detemine the size of an array in Java and VB I would enter:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. anArray.length

What is the Pascal equivalent?? I have looked arround the web and found no info. Is there no such equivalent??

Thanks for your kind help

Andrew.
Last edited by Scottie_uk; Sep 29th, 2006 at 3:03 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Scottie_uk is offline Offline
1 posts
since Sep 2006
Sep 30th, 2006
0

Re: Determining an Array's Length in Pascal

I've not used Delphi for a while but is it something like

Pascal and Delphi Syntax (Toggle Plain Text)
  1. Length(anArray)
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Oct 8th, 2006
0

Re: Determining an Array's Length in Pascal

Click to Expand / Collapse  Quote originally posted by Scottie_uk ...
I am a mainly Java and VB programmer. However, I am writing a program that generates computer code in various languages. One of these is Pascal.

What I am asking is simple.

To detemine the size of an array in Java and VB I would enter:

Pascal and Delphi Syntax (Toggle Plain Text)
  1. anArray.length
What is the Pascal equivalent?? I have looked arround the web and found no info. Is there no such equivalent??

Thanks for your kind help

Andrew.
Use Low() and High() to find the lowest element or highest element index, respectively, of an array.

code
Last edited by codewritinfool; Oct 8th, 2006 at 2:17 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
codewritinfool is offline Offline
5 posts
since Oct 2006
Dec 31st, 2006
0

Re: Determining an Array's Length in Pascal

in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
needs_help is offline Offline
59 posts
since Dec 2006
Dec 31st, 2006
0

Re: Determining an Array's Length in Pascal

Click to Expand / Collapse  Quote originally posted by needs_help ...
in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.
NOT TRUE. Free Pascal permits dynamic arrays defined like this:

Type
TByteArray : Array of Byte;
Var
A : TByteArray;

SetLength(A,1000);


After a call to SetLength, valid array indexes are 0 to 999: the array index is always zero-based.

Borland Object Pascal (Delphi) permits dynamic arrays defined like this:

var
byteArray : Array of Byte;

SetLength(byteArray, 5);



Reputation Points: 10
Solved Threads: 0
Newbie Poster
codewritinfool is offline Offline
5 posts
since Oct 2006
Dec 31st, 2006
0

Re: Determining an Array's Length in Pascal

Ok, but in dev-pascal, you can't change the length.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
needs_help is offline Offline
59 posts
since Dec 2006
Dec 31st, 2006
0

Re: Determining an Array's Length in Pascal

Click to Expand / Collapse  Quote originally posted by needs_help ...
Ok, but in dev-pascal, you can't change the length.
dev-pascal is an IDE, not a compiler. It commonly uses Free Pascal or GNU Pascal.

I've already shown how to do it in Free Pascal.

In GNU Pascal, you can use Schemata:

http://www.mirrorservice.org/sites/w...schemademo.pas
Reputation Points: 10
Solved Threads: 0
Newbie Poster
codewritinfool is offline Offline
5 posts
since Oct 2006
Oct 23rd, 2011
0
Re: Determining an Array's Length in Pascal
it is declared at the beginning in the form:
"array name" : array["lower limit".."upper limit"] of string;
(The quotation marks are where you put in your own thing but do not put the quotation marks in the actual program.)
Eg.
Favourite_car : array[1..10] of string;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fhgfjhgfhgfh is offline Offline
1 posts
since Oct 2011
Oct 24th, 2011
0
Re: Determining an Array's Length in Pascal
array lenght = size of the element type * index

You can find the size of the array in the memory as I show it above. Delphi (object pascal) has dinamic array, that we can set the length by the SetLength(array_name, element_number) procedure, so the elements starting from 0 to element_number-1. And as said, High(array_name) the upper indenx of the array, and the Low(array_name) the lowest.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Dec 8th, 2011
0

Not entierly so :-)

Click to Expand / Collapse  Quote originally posted by needs_help ...
in pascal, you have to predefine all your variable types, including array lengths, so you should already know the length.
For most programs we need to write, this may be true.
There are however many cases where we need to find the length of an array.

An array can be defined as open.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. VAR
  2. Example:ARRAY OF STRING;
  3. Example2:ARRAY OF ARRAY OF INTEGER;
These arrays are resizeable in runtime :-)
Pascal and Delphi Syntax (Toggle Plain Text)
  1. SetLength(Example,10);
  2. SetLength(Example2,10,10);
A multidimensional array is nice to use this way. Each sub array can be defined separately.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. SetLength(Example2,5);
  2. SetLength(Example2[0],10);
  3. SetLength(Example2[1],6);
  4. SetLength(Example2[2],90);
  5. SetLength(Example2[3],12);
  6. SetLength(Example2[4],19);

Instead of creating an array that is holding all the possible records our program can possibly need, we can design the program to only use as much memory as is actually needed on demand.

In these cases with SetLength, it makes sense to use the command HIGH(Example2[4]) in order to read out its set length.
Since SetLength always start an array at [0], the command LOW will always return Zero.

Pascal and Delphi Syntax (Toggle Plain Text)
  1. PROCEDURE SomeParameters(CONST Params:ARRAY OF INTEGER);
  2. VAR
  3. x:BYTE;
  4. BEGIN
  5. // HIGH(Params) will return the number of integers passed in the Params array.
  6. FOR x:=0 TO HIGH(Params) DO BEGIN
  7. // Code for whatever you would like to do with the parameters.
  8. END;
  9. END;
And again... When an array is passed as parameters, to my knowledge it always start with [0], so LOW(Params) will always return Zero.

IF you however declare an array from scratch in VAR section of program.
Pascal and Delphi Syntax (Toggle Plain Text)
  1. VAR
  2. Example : ARRAY[7..14];
  3. BEGIN
  4. LOW(Example)// Should return 7, but we know this already as it is already defined.
  5. END.

Best regards.
Reputation Points: 38
Solved Threads: 2
Light Poster
Morten Brendefu is offline Offline
44 posts
since Mar 2011
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Pascal: Maximum value in loop results
Next Thread in Pascal and Delphi Forum Timeline: Empty Object Variables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC