[fpc-pascal] How to split file of whitespace separated numbers?

Bo Berglund bo.berglund at gmail.com
Fri Dec 23 09:14:45 CET 2016


Is there a quick way to split a string of whitespace separated values
into the separate members?
I have to create a function to process a number of big data files
where
numbers are stored in lines of 4-6 values using whitespace inbetween.
First I got a sample looking like this:
{code}
0.4167    0.3636    -14.1483    227.2260
{code}
Here the separators were 4 spaces so on each line I used (slDecode is
a TStringList):
{code}
  sLine := StringReplace(sLine, '    ', #13, [rfReplaceAll]);
  slDecode.Text := sLine;
{code}
Worked fine if a bit slow...
The stringlist items are then passed to a string to float function and
stored into a dynamic array.

But then it failed on a file containing lines like this:
{code}
   0.000    0.000    7.000    0.000  29.6628
{code}
Here there are 3 leading spaces plus one separator is only 2 spaces
wide. So I had to modify the code:
{code}
  sLine := Trim(sLine);
  sLine := StringReplace(sLine, '    ', #13, [rfReplaceAll]);
  sLine := StringReplace(sLine, '  ', #13, [rfReplaceAll]);
  slDecode.Text := sLine;
{code}

This works in this case but now I realize I need something better,
which can deal with varying number of whitespace chars inbetween
numbers.
The test files are very big, like half a million lines and up, so I
cannot introduce a lot of code in the loop since processing time will
increase.

Is there any good and quick way to extract real data from a space
separated list without knowing beforehand the size of the whitespace
separators?

I guess that my next sample problem will be a file with TAB rather
than space or even mixed TAB and space...

-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list