user3480367
user3480367

Reputation: 9

Converting a list of numbers in a string to an array

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!

Upvotes: 0

Views: 3112

Answers (3)

deColaman
deColaman

Reputation: 223

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.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613451

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

Upvotes: 2

Stijn Sanders
Stijn Sanders

Reputation: 36850

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;

Upvotes: 6

Related Questions