Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 03-20-2002, 12:35 PM
klo klo is offline
Junior Member
 
Join Date: Jan 2002
Posts: 27
Default Using array size with for loops?

Hi
Sorry about the length.
is it possible in delphi to use the array length in a for loop. For example, I have allocated the size of the array as 0..100 but may only populate the 1st 3 positions. Rather than looping through my 'for' loop 100 times(seems like wasted process) and eliminating positions which are empty can I use an arrayLength property.
Code below may better explain my goal.

testArray: array [0..100] of String;

// assume that the array contains - 0='jon',1='dave',2='bob'

// the following is what i currently use
for i:=0 to 100 do
if testArray[i] <> '' then
string :=testArray[i];

//what i would like to use, don't know if there is anything
//like it but something that would use the length of the
//filled values only
// this loop would ideally only be carried out 3 times

for i:=0 to testArray.length // length(testArray) ??
string:=testArray[i];



Thanks,
Reply With Quote
  #2  
Old 03-20-2002, 12:39 PM
GoodFun GoodFun is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,645
Default RE: Using array size with for loops?

you can use the High and Low command:

High
Returns the highest value in the range of an argument.

Unit

System

Category

miscellaneous routines

function High(X);

Description

Call High to obtain the upper limit of an Ordinal, Array, or string value. The result type is X, or the index type of X.

X is either a type identifier or a variable reference. The type denoted by X, or the type of the variable denoted by X, must be one of the following:

For this type High returns

Ordinal type (includes Int64) The highest value in the range of the type

Array type The highest value within the range of the index type of the array

String type The declared size of the string
Open array The value, of type Integer, giving the number of elements in the actual parameter minus one
String parameter The value, of type Integer, giving the number of elements in the actual parameter minus one

Hope this helps,

Marcel

Please click on Accept if this helped...
Reply With Quote
  #3  
Old 03-20-2002, 01:00 PM
klo klo is offline
Junior Member
 
Join Date: Jan 2002
Posts: 27
Default RE: Using array size with for loops?

Unless I've missed something,

high(testArray) = 100
low(testArray) = 0

My goal is to only execute the for loop as many times as the number of filled values (3 in the example)
Thanks for the quick response though.
Reply With Quote
  #4  
Old 03-20-2002, 01:10 PM
GoodFun GoodFun is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,645
Default RE: Using array size with for loops?

Have a look at dynamic arrays... you can resize them to the number of values you have OR... use a TObjectList and add objects with the data in it to it, that's more efficient anyways.

Hope this helps,

Marcel

Please click on Accept if this helped...
Reply With Quote
  #5  
Old 03-20-2002, 01:13 PM
GoodFun GoodFun is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,645
Default RE: Using array size with for loops?

I just saw that you are using Strings in that array, in that case, you want to look at TStringList. It lets you add, remove, sort and do many other things with Strings AND it has a StringList.Count property. It's 100% dynamic and will make things ALOT easier for you.

Hope this helps,

Marcel

Please click on Accept if this helped...
Reply With Quote
  #6  
Old 03-20-2002, 09:13 PM
DarkHorse DarkHorse is offline
Senior Member
 
Join Date: Dec 2000
Posts: 1,636
Default RE: Using array size with for loops?

Hi,

Try this code. U can declare a dynamic array whose size varies at run time depending on the data it holds. Thats the best way to deal with arrays.

procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyArr: Array of Integer; // Dynamic Array
i: Integer;
begin
for i := 0 to 9 do
begin
SetLength(MyArr, High(MyArr) + 2); // Here the array
// increase size based on ur requirement.
MyArr[i] := i + 1;
end;

for i := Low(MyArr) to High(MyArr) do
Showmessage(IntToStr(MyArr[i]));

end;


Hope this helps. ;-))

Pls. accept if it helped.

Thanx
Tomy.
Reply With Quote
  #7  
Old 03-20-2002, 09:43 PM
GoodFun GoodFun is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,645
Default Example for StringList...

Hi again,

just a little example on what you can do with a StringList:

first you need to define it somewhere, probably your mainform.

Uses Classes; // and what ever else you need

var NameList: TStringList;

Then you have to create the object, most likely in your MainForm.OnCreate event.

NameList := TStringList.Create;

To make things clean, you also have to free the object again before you leave your application. You most likely would do that in your MainForm.OnClose event:

NameList.Free;

Now you got the object, how do you work with it...
Let's add some strings by hand...

NameList.Add('Jon');
NameList.Add('Dave');
NameList.Add('Bob');

The NameList has now 3 entries with the names specified above. To access them again you use:

var i: Integer;

for i := 0 to NameList.Count - 1 do
begin
ShowMessage(NameList.Strings[i]);
end;

Now we want to have those names in the right order, let's sort them:

NameList.Sort;

if you execute the for loop once more that we used above, you will see that the entries are now alphabetically ordered.
Now let's search for a name in that list:

var FoundIndex: Integer;
var SearchName: String;

SearchName := 'Bob';

if (NameList.Find(SearchName, FoundIndex)) then
begin
ShowMessage('Found Name ' + NameList.Strings[FoundIndex] +
' at Index ' + IntToStr(FoundIndex));
end
else
begin
ShowMessage(SearchName + ' not found in the name list');
end;

Now with the Index that we just got above, we can delete Bob from our list:

NameList.Delete(FoundIndex);

To clear the whole list, use:

NameList.Clear;

and finally, to load a list from a file, you can use:

NameList.LoadFromFile('MyNames.txt');

Well, I hope that gave you a nice little introduction on how to use StringLists. There are more things you can do with it like use Name=Value pairs and access them through the Names[Index] and Values[Index] property or you can attach objects to a certain name to store more information than just a name. In that case, you have to make sure that you destroy all the objects that you attached before you destroy the list when you are done with it.


Hope this helps,

Marcel

Please click on Accept if this helped...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 07:48 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2014, vBulletin Solutions, Inc.