[fpc-pascal] How to split file of whitespace separated numbers?
Luiz Americo Pereira Camara
luizamericop at gmail.com
Sat Dec 24 11:55:32 CET 2016
Em 23 de dez de 2016 05:15, "Bo Berglund" <bo.berglund at gmail.com> escreveu:
Is there a quick way to split a string of whitespace separated values
into the separate members?
Unir strutils
Wordcount + extractword
Or
Extractsubstr in loop
http://www.freepascal.org/docs-html/rtl/strutils/extractsubstr.html
Luiz
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
_______________________________________________
fpc-pascal maillist - fpc-pascal at lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20161224/9e4344b9/attachment.html>
More information about the fpc-pascal
mailing list