Array Function

I need an array function that will take a string and then split the string into the array elements by a specified separator. The function would return the filled array.

I can write the function, my problem is declaring the function and using the function. I thought it could be written as:

function ParseData(dataLine: String; sep: String): Array of String;

then the use of the array would be:

parsedArray := ParseData(dataLine, sep);

I get all sorts of errors when trying the above. Can someone show me how to utilize arrays as parameters and results?

Thanks in advance,
Chad
dhertzfeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhertzfeAuthor Commented:
Edited text of question.
0
MotazCommented:
Hello Chad, you can change it to procedure and you have to pass the array result as a parameter, the procedure header will be:

procedure ParseArray(dataLine: String; sep: String; var x: array of string);

you can call it as:


var
  MyArray: Array of string;
begin
  ParseArray('This is a test', ' ',
    MyArray);

Motaz

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MotazCommented:
Instead of using array of string you have to use TStringList, the full function is :

procedure GetToken(DataLine: string; sep: char; var x: TStringList);
var
  i: Integer;
begin
  i:= 0;
  if DataLine[Length(DataLine)] <> Sep then
    DataLine:= DataLine + Sep;
  repeat
    x.Add(Copy(DataLine, 1, Pos(Sep, DataLine) - 1));
    Delete(DataLine, 1, Pos(Sep, DataLine) );
  until DataLine = '';

end;


procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStringList;
begin
  List:= TStringList.Create;
  GetToken('First,Second,Third,Last', ',', List);
  Memo1.Lines:= List;

end;

Motaz
0
dhertzfeAuthor Commented:
Thanks Motaz,

This helps alot.

Chad
0
MadshiCommented:
If you have D4 or D5, you can also use this (which is quite near to your first try):

type
  TDAString = array of string;  // dynamic array of string

function ParseData(dataLine, sep: String): TDAString;
begin
  // something like this:
  SetLength(result, 10);
  result[0] := 'firstElement';
  result[1] := 'secondElement';
  // ...
end;

var parsedArray : TDAString;

parsedArray := ParseData(dataLine, sep);
for i1 := 0 to high(parsedArray) do
  ShowMessage(parsedArray[i1]);

Regards, Madshi.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.