Recent

Author Topic: Eliminating empty strings in TStringList when reading from text file.  (Read 22020 times)

Bart

  • Hero Member
  • *****
  • Posts: 5537
    • Bart en Mariska's Webstek
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #60 on: March 14, 2020, 04:45:23 pm »
If I understand the test code correclty, then each test loads the file from disk.
Since disk-reads are cached by the OS, this isn't really fair, at least not for the first testmethod that is called.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5537
    • Bart en Mariska's Webstek
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #61 on: March 14, 2020, 04:47:32 pm »
You obviously are using something that looks like a blank line but isn't...

The part of the code that checks for failure uses a simple check for a string being completely empty.

Bart

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #62 on: March 14, 2020, 04:57:04 pm »
Please provide a file!!!!!!!!!!!!!!!!!!!!

Save the result of the CreateStringList function to a file and you will get the required one.

@Bart, please note that the first file load is only for counting non-empty lines.

jamie

  • Hero Member
  • *****
  • Posts: 6823
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #63 on: March 14, 2020, 04:57:52 pm »
I completely remove the line Endings so when the chunk of memory is complete its gone!

This can be verified by the fact that I have both a Write and Read pointers, at the write pointer is far behind the read pointer and thus a NUL is placed at the end of the stream.

 Using the debugger will verify this by stepping through it via the ST pointer I put there only for debugging usage.
   
  Over here it works fine but I use  MEMO to view the results and it's all working fine..

 I need the same file he is using for the test, I suspect there could be control or other chars in there that are not normally visible
The only true wisdom is knowing you know nothing

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #64 on: March 14, 2020, 05:06:01 pm »
@jamie, did you read my post #62?

BrunoK

  • Hero Member
  • *****
  • Posts: 682
  • Retired programmer
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #65 on: March 14, 2020, 05:06:59 pm »
My entry (as long as it complies with the benchmark conditions ...)
Code: Pascal  [Select][+][-]
  1.   procedure RemovEmptyLinesBruno(aList: TStringList);
  2.   var
  3.     lList : TStringList;
  4.     lString : string;
  5.     i : integer;
  6.   begin
  7.     lList := TStringList.Create;
  8.     lList.Assign(aList);
  9.     aList.Clear;
  10.     for i := 0 to lList.Count - 1 do begin
  11.       lString := lList.Strings[i];
  12.       if lString<>'' then
  13.         aList.Add(lString);
  14.     end;
  15.     lList.Free;
  16.   end;
  17.  

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #66 on: March 14, 2020, 05:15:37 pm »
Done:
Code: Text  [Select][+][-]
  1. DelEmptyLinesMax        elapsed time(ms): 3385
  2. DelEmptyLinesBart       elapsed time(ms): 2521
  3. DelEmptyLinesThaddy     elapsed time(ms): 75 (failure)
  4. DelEmptyLinesWinny      elapsed time(ms): 3147
  5. DelEmptyLinesAvk        elapsed time(ms): 67
  6. DelEmptyLinesJamie      elapsed time(ms): 23 (failure)
  7. DelEmptyLinesEgsuh      elapsed time(ms): 78
  8. DelEmptyLinesBruno      elapsed time(ms): 48
  9.  

jamie

  • Hero Member
  • *****
  • Posts: 6823
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #67 on: March 14, 2020, 05:17:45 pm »
This conversion is done on my end.

You refuse to supply the same material you are using and what I use is working perfectly...

You guys carry on, its nothing but a MOOT subject to me now.

Nothing but noise!
The only true wisdom is knowing you know nothing

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #68 on: March 14, 2020, 05:27:55 pm »
@jamie, the maximum permissible size of a file uploaded to the forum is 250KB. The test file is much larger.

Bart

  • Hero Member
  • *****
  • Posts: 5537
    • Bart en Mariska's Webstek
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #69 on: March 14, 2020, 05:34:21 pm »
Thaddy's code misses the last empty line.
Jamie: the file contains @194635:
e_D 6IJglfY1F6xs


82Q0H0hkEJvt9OT_

Your code misses that somehow.

The file can be build by adding this code at the beginning of the program:
Code: Pascal  [Select][+][-]
  1.   TestList := CreateStringList;
  2.   TestList.SaveToFile(TestFile);
  3.   TestList.Free;

Since RandSeed is set, it should give the same file on each run.
(I actually created it only once)

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5537
    • Bart en Mariska's Webstek
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #70 on: March 14, 2020, 05:39:51 pm »
I've modified the code somewhat so HasEmptyLines gives a reference to what is finds:
It will return the (zero based) index of the empty line it finds in the result and will display the preceding, offending and next line.
<EOList>  means that the found empty line is the last line in the list.

Code: [Select]
DelEmptyLinesMax        elapsed time(ms): 5618
DelEmptyLinesBart       elapsed time(ms): 4439
DelEmptyLinesThaddy     elapsed time(ms): 85 (failure "") @160252, ["xunlT3c7O9DYwtfl", "", "<EOList>"]
DelEmptyLinesWinny      elapsed time(ms): 10714
DelEmptyLinesJamie      elapsed time(ms): 53 (failure "") @160254, ["e_D 6IJglfY1F6xs", "", ""]
DelEmptyLinesEgsuh      elapsed time(ms): 229

I could not test Avk's method because of missing units.

I also write the resulting stringlist to a file and I can indeed see empty lines in the result of Jamies method.

Bart
« Last Edit: March 14, 2020, 05:44:40 pm by Bart »

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #71 on: March 14, 2020, 05:54:06 pm »
Thaddy's code misses the last empty line.
There is an option for that. SkipLastEmpty.
But I am sure they don't want the Trumps back...

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #72 on: March 14, 2020, 05:59:05 pm »
@Bart, just curious on which machine you tested?

Bart

  • Hero Member
  • *****
  • Posts: 5537
    • Bart en Mariska's Webstek
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #73 on: March 14, 2020, 06:05:56 pm »
@Bart, just curious on which machine you tested?

Win10, 32-bit fpc.

Bart

avk

  • Hero Member
  • *****
  • Posts: 771
Re: Eliminating empty strings in TStringList when reading from text file.
« Reply #74 on: March 14, 2020, 06:17:08 pm »
Thank you, now I also want to try the 32-bit version.

 

TinyPortal © 2005-2018