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

greim greim at schleibinger.com
Fri Dec 23 15:12:08 CET 2016


Hey Kids,

why so complicated?

Good old Niklaus Wirth has already everything done for you:
I have to cite one sentence on the last slide at his birthday colloquium:

"Reducing size and complexity is the triumph"

So READ is already quite clever, it doesn't care about whitespaces, 
carriage returns and linefeeds :


PROGRAM readline;

VAR a  : ARRAY[0..1000000] OF double;
     infile : TEXT;
     lauf, lauf2 : longint;

BEGIN
lauf := 0;
assign(infile, 'infile.txt');
reset(infile);
WHILE NOT(eof(infile)) DO

         BEGIN
         read(infile, a[lauf]);
         inc(lauf);
         END;

close(infile);

FOR lauf2 := 0 TO pred(lauf) DO

         BEGIN
         writeln('Index : ', lauf2, ' Value : ', a[lauf2]);
         END;


END.


And here infile.txt:

  123.4   55.2 33.1 4
  12.1 1.1
1 2 3 4
     333.888 444.555

Regards

Markus


















Am 23.12.2016 um 14:06 schrieb Sven Barth:
> Am 23.12.2016 12:54 schrieb "Graeme Geldenhuys"
> <mailinglists at geldenhuys.co.uk <mailto:mailinglists at geldenhuys.co.uk>>:
>>
>> On 2016-12-23 08:14, Bo Berglund wrote:
>> > Is there a quick way to split a string of whitespace separated values
>> > into the separate members?
>>
>>
>> That problem is perfectly suited for regular expressions. And a rather
>> simple one at than. The FPC's FCL packages include a regex unit too
>> which should suite your needs.
>>
>>
>> http://www.regex101.com/
>>
>> http://www.regexplained.co.uk/
>>
>> http://regex.info/
>>   Even the trial book (first chapter only) of "Mastering Regular
>>   Expressions" is invaluable for users new to regex. And will
>>   explain all you need to know to solve your problem.
>>
>
> Regular expressions usually have a higher overhead however (as you might
> have noticed, Bo timed his code later on).
> For example at work I changed a regular expression based parser for the
> lines of a log file to a simpler one and the speedup was noticeable (I
> don't have exact numbers anymore however).
>
> Regards,
> Sven
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>




More information about the fpc-pascal mailing list