0

I am given 21 y-coordinates of a function in a .txt file (a single string when read by Delphi) which are written vertically in .txt document. How do I split this string into its individual values and store them in an array "Farr"? The text document contents look like this:

-3
-2.5
-2
...

Each value needs to be stored into a regular array of 0...21 like so:

Farr:=[-3, -2.5, -2, ...]

Thanks!

LU RD
  • 34,438
  • 5
  • 88
  • 296
user3480367
  • 9
  • 1
  • 4
  • So what is your question? – David Heffernan Mar 31 '14 at 08:54
  • Sorry if I wasn't specific enough. I"m not sure how I should convert the individual numbers in the string to an array list. – user3480367 Mar 31 '14 at 09:01
  • What is an array list? How do you want to convert them? What are the possible formats for the numbers? Real? Integer? What decimal point is to be used? Have you written any code? Do you know how to write code? Are you an expert programmer, or a total novice? How can we help you without more information? – David Heffernan Mar 31 '14 at 09:03
  • Ive edited the original question, the rest is free to be determined. Hopefully this is sufficient. I'm also quite new to Delphi. Thanks! – user3480367 Mar 31 '14 at 09:09
  • You say there are 21 numbers, and the array indices should be 0..21. Do you mean 0..20? – David Dubois Mar 31 '14 at 09:46

3 Answers3

6

Try this:

var
  sl: TStringList;
  i: integer;
  s: string;
  arr: array of double;
begin
  sl := TStringList.Create;
  try
    sl.LoadFromFile(FilePath);
    SetLength(arr, sl.Count);
    for i := 0 to sl.Count - 1 do
      arr[i] := StrToFloat(sl[i]);
  finally
    sl.Free;
  end;
end;
Kromster
  • 7,181
  • 7
  • 63
  • 111
Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
2

Note: When I wrote this answer, the question claimed that multiple values appeared in a single line, separated by spaces. Apparently that information was wrong.

There are multiple parts to this question. Please do read this answer in consultation with the documentation. Some hopefully useful tips on doing that can be found here: How can I search for Delphi documentation?

Reading a file

Lots of ways to do this. Perhaps the simplest and most common approach is to use a TStringList. Create one, and call LoadFromFile passing the name of your text file.

Splitting the string

Again, plenty of options. I'd probably go for a split function like StrUtils.SplitString.

If you have a string str containing the space-delimited values you would do the following:

var
  Values: TStringDynArray;
....
Values := SplitString(str, ' ');

Of course, it now looks like you have one value per line of the file. In which case you don't need to split any strings.

Converting values from string to real

Use StrToFloat for this.

var
  RealValue: Real;
....
RealValue := StrToFloat(StringValue);

This will use the locale settings of the local user. That might give you a headache if there's a mismatch with the decimal separators. If the decimal separator is always going to be, for instance, ., then you need to specify that.

var
  RealValue: Real;
  FormatSettings: TFormatSettings;
....
FormatSettings := TFormatSettings.Create;
FormatSettings.DecimalSeparator := '.';
RealValue := StrToFloat(StringValue, FormatSettings);

If StrToFloat fails for any reason it will raise an exception. As an alternative you can use TryStrToFloat which indicates failure using its Boolean return value. For example:

if not TryStrToFloat(StringValue, RealValue, FormatSettings) then
  .... deal with error
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

You can use a regular expression to get the number of matches and the matches themselves. Here's the code to do so, although you may want to change the regular expression:

MyRegexp := TRegEx.Create('[-+]?[0-9]*\.?[0-9]+');
with MyRegexp.Matches do
  if Count = 21 then //As you always want 21 items
    for i := 0 to 20 do
      FArr[i] := StrToFloat(Item[i]);

Of course Error handling an such should be added, but this is the basic of what you'd want. Also, it's ok if the list is separated by spaces, semicolons, etc, depending on you regular expression.

deColaman
  • 213
  • 2
  • 14