How to split file of whitespace separated numbers?

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: How to split file of whitespace separated numbers?

greim
Hallo Bo,

please try the simple

READ(myfile, x);

as mentioned in my posting from 23.dec.
It works w/o any postprocessing.

Markus

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to split file of whitespace separated numbers?

Bo Berglund
On Sat, 31 Dec 2016 11:27:53 +0100, greim
<[hidden email]> wrote:

>Hallo Bo,
>
>please try the simple
>
>READ(myfile, x);
>
>as mentioned in my posting from 23.dec.
>It works w/o any postprocessing.
>
I saw that but the problem is that there are varying number of items
on each line and a line is considered a record.
So I would anyway have to keep track of the lines.
With Read I guess it skips all whitespace including line endings and
this causes problems in identifying the content because of varying
number of items on each line.
The Readln approach followed by splitting in a stringlist is enough of
an improvement that I can use it.


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to split file of whitespace separated numbers?

Luiz Americo Pereira Camara-3


2016-12-31 8:18 GMT-03:00 Bo Berglund <[hidden email]>:
On Sat, 31 Dec 2016 11:27:53 +0100, greim

The Readln approach followed by splitting in a stringlist is enough of
an improvement that I can use it.


Did you look at StrUtils.ExtractSubStr ?

Should be faster than TStrings;

PosWord := 1;
Word := ExtractSubstr(Line, PosWord, [' ']);
while (Word <> '') do
begin
  //do work
  Word := ExtractSubstr(Line, PosWord, [' ']);
end;

Luiz

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to split file of whitespace separated numbers?

Bo Berglund
On Sat, 31 Dec 2016 11:30:26 -0300, Luiz Americo Pereira Camara
<[hidden email]> wrote:

>2016-12-31 8:18 GMT-03:00 Bo Berglund <[hidden email]>:
>
>> On Sat, 31 Dec 2016 11:27:53 +0100, greim
>>
>> The Readln approach followed by splitting in a stringlist is enough of
>> an improvement that I can use it.
>>
>>
>Did you look at StrUtils.ExtractSubStr ?
>
>Should be faster than TStrings;
>
>PosWord := 1;
>Word := ExtractSubstr(Line, PosWord, [' ']);
>while (Word <> '') do
>begin
>  //do work
>  Word := ExtractSubstr(Line, PosWord, [' ']);
>end;

Thanks, I did not know about this...
Unfortunately the function it is not part of the Delphi StrUtils unit
and I need my solution to be usable on Delphi since the bulk of the
program is probably too difficult to port to FPC. It contains a lot of
graphics that uses an outdated version of GLScene, which I have had to
make some minimal pathces to in order to port the program up to XE5.
But I feel a port to FPC would tax my abilities too much because of
the 3rd party stuff that has been used...

It was a good suggestion, though!
And I see that the delimiter argument can be set to StdWordDelims,
which takes care of virtually all whitespace stuff. Will use it
whenever I code in FPC and have this problem.


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to split file of whitespace separated numbers?

greim
In reply to this post by Bo Berglund
Hallo Bo,

You are right. If you want to keep the numbers of one line together,
then READ doesn't work for you.

Markus






Am 31.12.2016 um 12:18 schrieb Bo Berglund:

> On Sat, 31 Dec 2016 11:27:53 +0100, greim
> <[hidden email]> wrote:
>
>> Hallo Bo,
>>
>> please try the simple
>>
>> READ(myfile, x);
>>
>> as mentioned in my posting from 23.dec.
>> It works w/o any postprocessing.
>>
> I saw that but the problem is that there are varying number of items
> on each line and a line is considered a record.
> So I would anyway have to keep track of the lines.
> With Read I guess it skips all whitespace including line endings and
> this causes problems in identifying the content because of varying
> number of items on each line.
> The Readln approach followed by splitting in a stringlist is enough of
> an improvement that I can use it.
>
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12