Recent

Author Topic: reading data from a text file :)  (Read 362 times)

speter

  • Full Member
  • ***
  • Posts: 161
reading data from a text file :)
« on: January 25, 2021, 06:31:59 am »
G'Day Folks,

I am reading a data file formatted like:

Code: Pascal  [Select][+][-]
  1. ((35,0),
  2.   (41, 14), (44, 13), (54, 16), (52, 20), (59, 24), (63, 29), (65, 29), (65, 35), (67, 41), (65, 42),
  3.   (54, 39), (59, 44), (59, 50), (63, 55), (63, 65), (57, 59), (52, 48), (48, 59), (41, 57), (33, 65),
  4.   (29, 61), (37, 57), (33, 50), (37, 44), (33, 41), (35, 33), (18, 46), (11, 41), (16, 35), ( 7, 24),
  5.   ( 9, 16), (19, 11), (27, 14), (33, 12), (41, 14), (0,  0), (0,  0), (0,  0), (0,  0), (0,  0)),
  6.  
  7.  ((16,0),
  8.   (36, 27), (50, 31), (46, 39), (31, 42), (39, 48), (42, 44), (52, 41), (50, 48), (47, 56), (37, 59),
  9.   (36, 53), (29, 50), (24, 52), (20, 41), (24, 29), (36, 27), (0, 0), (0, 0), (0, 0), (0, 0),
  10.   (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0),
  11.   (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)),
  12.  
  13. (and so on...)
  14.  

at present I am reading each line then parsing the (x,y) points:

Code: Pascal  [Select][+][-]
  1.   function getpoint : tpoint;
  2.   var
  3.     k1,k2,k3 : byte;
  4.     n1,n2 : string;
  5.   begin
  6.     error := '';
  7.     k1 := pos('((',s) + 1;
  8.     if k1=0 then
  9.       k1 := pos('(',s);
  10.     k2 := pos(',',s);
  11.     k3 := pos('),',s);
  12.  
  13.     result := point(0,0);
  14.     if (k1=0) or (k2=0) or (k3=0) then
  15.       error := ' did not find (...),'
  16.     else if (k1 < k2) and (k2 < k3) then
  17.       begin
  18.         n1 := copy(s,k1+1,k2-k1-1);
  19.         n2 := copy(s,k2+1,k3-k2-1);
  20.         result := point(strtoint(n1),strtoint(n2));
  21.       end
  22.     else
  23.       error := ' not k1 < k2 < k3';
  24.   end;

This is working, but I thought I'd do a sanity check :) to check whether there might be a simpler way.
The data pattern is consistent (1 point, 4 x 10 points, blank line). ;)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

jamie

  • Hero Member
  • *****
  • Posts: 4189
Re: reading data from a text file :)
« Reply #1 on: January 29, 2021, 02:08:02 am »
yes there is a much better way..

if that is a constant array field (a Writeable one) then you can write the complete contents out to disk using blockWrite, BlockRead etc..

But keep in mind that you can not change the array structure dynamically and expect it to load properly from one that has a different tree structure..

But if looks like you are stuck on Text files, poor choice for that kind of structure..


The only true wisdom is knowing you know nothing

speter

  • Full Member
  • ***
  • Posts: 161
Re: reading data from a text file :)
« Reply #2 on: January 29, 2021, 06:38:20 am »
Jamie, thanks for the reply.

Yes, the data is in a text file.

I eventually converted the data into a comma delimited list; which simplified everything.

cheers
S. :)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

 

TinyPortal © 2005-2018