Recent

Author Topic: Reading data files that are Chaotic.  (Read 1517 times)

TRon

  • Hero Member
  • *****
  • Posts: 4371
Re: Reading data files that are Chaotic.
« Reply #15 on: April 21, 2025, 02:03:36 pm »
I even checked the data, it's all in the right place.  I checked about 10 records in different parts of the file and they all come out right the way they should.  All the data is in the right fields.
Impossible with code as shown in that message and taking into account the initially shared specifications :o
Today is tomorrow's yesterday.

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: Reading data files that are Chaotic.
« Reply #16 on: April 21, 2025, 05:12:29 pm »
I just can't see what's wrong.

I didn't analyze what your are trying to do in your code, but it isn't correct for sure.

I.e.:
Code: Pascal  [Select][+][-]
  1.         If i < sl.Count Then Inc(i);
  2.         Trail := Trim(sl[i]);

You are checking if variable 'i' is less then sl.Count.

Let's assume sl.Count is 100 and variable 'i' is 99. So you increase it to 100.

In next line you are accessing string list with sl[ i ]. Index here shouldn't be larger than 99.

And you have many of these increments in your code.

Maybe you should do it differently, something like this:
Code: Pascal  [Select][+][-]
  1.         Inc(i);
  2.         If i >= sl.Count Then Exit; // or Break or whatever you do when you are at the end
  3.         Trail := Trim(sl[i]);

Edit: I didn't use code tags with variable 'i'.
That did it.  I inserted your line 2 just after my line 135.  I just left off the ">" and just used the "=".  Boom!  Works like a charm!

Code: Pascal  [Select][+][-]
  1. Temp[x]:= Trim(sl[i]);
  2. Memo1.Lines.Add('Hereafter lies i: ' + IntToStr(i) + '.      And the count is: ' + IntToStr(sl.Count));
  3. If i < sl.Count Then Inc(i);    //  This is line 135.
  4. If i = sl.Count Then Exit;
  5. Memo1.Lines.Add('After the Inc i: ' + IntToStr(i) + '.      And the count is: ' + IntToStr(sl.Count));
  6. Inc(x);


Let's assume sl.Count is 100 and variable 'i' is 99. So you increase it to 100.
In next line you are accessing string list with sl[ i ]. Index here shouldn't be larger than 99.
This was what hit me.  I didn't even think about the Exit.
Thank you dseligo!




change all occurrences:
Code: Pascal  [Select][+][-]
  1. If i < sl.Count Then Inc(i);
to:
Code: Pascal  [Select][+][-]
  1. If i < sl.Count - 1 Then Inc(i);
I did that!  Before I posted.  That one really made it go crazy!
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

 

TinyPortal © 2005-2018