Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Elemen Index 2

Status
Not open for further replies.

cpmasesa

Programmer
Oct 24, 2002
78
AE
Hello,

I have an array arForm[1..15, 1..3]

I would like to find the position of an element in this array. (This array element is always in the 1st 'column' of my array.)

Suppose arForm[1,1] is 'Delphi'; arForm[2,1] is 'Inprise' and arForm[3,1] is 'Borland.

I want to be able to do something like IndexOf('Inprise') and get back 2 (from the above example)

Is this possible??

Clemens
 
If you use a memory table rather than array you can solve your question.
I advice you this 'free' component
Drag this component on your form, with right mouse button you can define structure ......

Use '.Locate' for find your element and '.ReNo' for it's position.

I hope this can help you.
For more ... post again

Giovanni Caramia
 
Sure. You'd have to write the function, but it's a simple enough matter to search the array for whatever string it is you are passing to the function.

function IndexOf(in_str: string): integer;
 
Giovanni,

Thank you for your tip. The memory table was able to solve my problem.

Clemens

 
Glen9999,

I have found a solution, thanks to Giovanni, that uses a memory table and it works very well.

However, you say "but it's a simple enough matter to search the array for whatever string it is you are passing to the function"

Do you mean that this is already possible without having to write my own function??

If not, can you please give me a rough idea as to hoe the function would be?

Thanks,

Clemens
 
Clemens,

If your array comprises only string values then use a TStringList as this has built-in functions for finding strings and returning the index.

Here is an excerpt from the Delphi help file which I have adapted to match your post:
Code:
procedure TForm1.FormCreate(Sender: TObject);
var
  MyList: TStringList;
  Index: Integer;
begin
  MyList := TStringList.Create;
  try
    MyList.Add('Delphi');
    MyList.Add('Inprise');
    MyList.Add('Borland');
    MyList.Sort; { Find will only work on sorted lists! }
    if MyList.Find('Borland', Index) then
    begin
      ListBox1.Items.AddStrings(MyList);
      Label1.Caption := 'Borland has an index value of ' + IntToStr(Index);
    end;
  finally
    MyList.Free;
  end;
end;

Hope this helps!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Do you mean that this is already possible without having to write my own function??"

Are people so skills-deficient that all they can do is put the tinker-toys together? Not to offend, but I'm just amazed someone could ask such a question.

function SearchTable(in_str: string): integer;
{ search 1-d array called "table" for in_str,
if found, return index value. Else return 0
indicating value was not found. Table_size is
the total number of elements in the table }
var
i: integer;
output: integer;
begin
output := 0;
for i := 1 to table_size do
if table = in_str then
output := i;
IndexOf := output;
end;

I typed that in...what? a minute? And while I would test it, I'm 99.9999% certain it's working.
 
Glenn9999,

Thanks for your response.

I doubt if the code will execute:
1. IndexOf refers to what??
2. Is output == Result??

Also you 'hard wire' the array size; table_size. It would be nice if the function could determine the array size.

On the other hand, the array might be a zero based array and hence for 1 := 1 to table_size might miss one element

But I could be wrong, let me try running your code and I will come back to you on this

Clemens.

 
I think Glen9999 was trying to give you an idea of what you need to do - rather than giving you a made-to-order function. You should try and modify it to your own needs and if you get stuck post back here.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks for the pointer Stretchwickster. Also, thanks Glen9999 for giving me an idea on what to do.

Here is the code that I came up with, thanks to Glen9999.

function SearchArray(s2Search4 : string; iColumn2Search : integer) : integer;
{s2Search4 is the string that we are looking for
iCol2Search is the column in the array that we want to search, min is a 2 dimensional array
arFamily is the array that we are searching}
var i : integer;
begin
Result := -1;
for i := low(arFamily) to high(arFamily) do
if arFamily[i,iCol2Search] = s2Search4 then
Result := i
end;

Problem, to make it even better, generic, I would like to tell the function what array to search in. i.e pass arFamily as a parameter.

Any ideas on how to do this??

Clemens
 
Arrays of strings are probobly best passed into funtions/procedures as a TStringlist. Stretchwickster has demonstrated useing this format.

e.g.
procedure TUpdate.ExpandNetList(var List: tstringlist);
Create the string list before passing it into the procedure

Or a function can return a stringlist

function TNetbackup.RecurseSubs(rootdir: string): Tstringlist;

You need to have a stringlist created to accept the output of the fuction which itsself must create the 'result' stringlist internaly (but not free it!)



Steve: Delphi a feersum engin indeed.
 
As Steve demonstrated, to pass what Borland call a "variable parameter" i.e. a parameter that you pass in to a function/procedure, change in some way and continue to use outside of the function/procedure you need to prefix it with the var keyword. The problem is Delphi doesn't allow you to pass "Array of..." into a function/procedure. To overcome this you need to declare an array type. Here's some example code:
Code:
type TStringArray = Array of String; // array dimensions need to be defined
or
Code:
type TStringArray = Array[1..10] of String; // array dimensions are preset
Then you can pass this new type in and out of your function:
Code:
function SearchArray([b]var AnArray: TStringArray; [/b]s2Search4 : string; iColumn2Search : integer) : integer;
var i : integer;
begin
  ...
end;

I hope this makes sense!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I meant to add one more thing. Creating a TStringArray might not be the best way to deal with this particular scenario since Delphi already provides you with a mechanism for handling a number of strings (TStringList) - I think this is the point Steve was making.

But ultimately it's up to you!

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thank you guys.

For my particular scenario, memory tables really seem to be the best solution.

Thanks a lot for your help, all especially Glen9999

Clemens
 
Yes, the problem lies in that Tstrings can have variable length.

But you can pass arrays of fixed data size objects into a function/procedure (an open array parameter)
the array of char is declared in the normal way.

var Head: array 0..65 of char;

Code:
DecodeWORD(head, 2);

function TPicturebrowser.DecodeWORD(values: array of char; index: integer): integer;
begin
   result :=  ord(values[index]) or (ord(values[index + 1]) shl 8);
end;

Steve: Delphi a feersum engin indeed.
 
sggaunt

Exactly the solution that I was looking for when I started this thread.

I can not seem to get similar code to work for a two dimensional array.

Say,

var foo : Array[1..5,1..3] of char

passing foo to a function procedure gives 'Incompatible Types'

So I suppose this can only be done for 1 dimensional arrays?

Clemens
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top