Recent

Author Topic: String Splicing?  (Read 3090 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
String Splicing?
« on: December 06, 2018, 12:24:04 am »
Have a listbox loaded with about 7,500 records that look like this:

They are delimited by ';'. Each with 6 records.

Is there a slick string function that will allow me extract the fields? I thought it would be easier if I replaced the ';' with a space but I see that won,t work because of line 4.
CYNTHIANA HARRISON CO

0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG
0B8;Elizabeth Field;41.25201;-72.03157;8;712;3C8
0CL8;Tera Skypark;35.59098;-117.6321;2490;1067;KEDW
0I8;CYNTHIANA HARRISON CO;38.36617;-84.28334;721;1171;KCVG
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: String Splicing?
« Reply #1 on: December 06, 2018, 12:56:34 am »
Is there a slick string function that will allow me extract the fields?
Use TStringList:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$LONGSTRINGS ON}
  3.  
  4. uses Classes;
  5.  
  6. const
  7.   CSomeContent =
  8.     '0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG' + LineEnding +
  9.     '0B8;Elizabeth Field;41.25201;-72.03157;8;712;3C8' + LineEnding +
  10.     '0CL8;Tera Skypark;35.59098;-117.6321;2490;1067;KEDW' + LineEnding +
  11.     '0I8;CYNTHIANA HARRISON CO;38.36617;-84.28334;721;1171;KCVG';
  12.  
  13. var
  14.   ContentList, LineList: TStringList;
  15.   Line, Field: string;
  16. begin
  17.   ContentList := TStringList.Create;
  18.   try
  19.     ContentList.Text := CSomeContent; // ContentList.LoadFromFile('...')
  20.     LineList := TStringList.Create;
  21.     try
  22.       LineList.Delimiter := ';';
  23.       for Line in ContentList do
  24.       begin
  25.         LineList.DelimitedText := Line;
  26.         for Field in LineList do
  27.           Writeln(Field);
  28.         Writeln('-------------');
  29.       end;
  30.     finally
  31.       LineList.Free;
  32.     end;
  33.   finally
  34.     ContentList.Free;
  35.   end;
  36.   Readln;
  37. end.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: String Splicing?
« Reply #2 on: December 06, 2018, 01:08:30 am »
You mean copy all 7,500 records into a program and declare them into a constant adding '+ LineEnding +' to each line.

I don't know?
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: String Splicing?
« Reply #3 on: December 06, 2018, 01:34:01 am »
You mean copy all 7,500 records into a program and declare them into a constant adding '+ LineEnding +' to each line.

I don't know?

ASerge provided an alternative in his example. Read the only comment in it.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: String Splicing?
« Reply #4 on: December 06, 2018, 01:49:28 am »
Yea, It's not an easy problem.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: String Splicing?
« Reply #5 on: December 06, 2018, 01:58:36 am »
Yea, It's not an easy problem.

Yeah, LoadFromFile is a real killer.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: String Splicing?
« Reply #6 on: December 06, 2018, 02:12:49 am »
Yea, It's not an easy problem.
It looks like you could use a good book on Pascal programming.   Without some understanding of the basics, you will find it difficult, not to mention frustrating, to get anything done.  If you're interested, pick up a copy of "Oh Pascal" by Doug Cooper, it's a really good book (basic programming, not object oriented, good, systematic analysis of problems and procedures so they can be represented in Pascal, very good place to start.)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: String Splicing?
« Reply #7 on: December 06, 2018, 02:13:58 am »
Well, the OP said he has the content in a TListBox, so let's change ASerge's code to:

Code: Pascal  [Select][+][-]
  1. procedure DecodeLines;
  2. var
  3.   LineList: TStringList;
  4.   Line, Field: string;
  5. begin
  6.   LineList := TStringList.Create;
  7.   try
  8.     LineList.Delimiter := ';';
  9.     { Run through the items in the ListBox ... }
  10.     for Line in TheListBox.Items do
  11.     begin
  12.       { For each item, let a TStringList "decode" the contents}
  13.       LineList.DelimitedText := Line;
  14.       { The StringList now has as many lines as fields
  15.         were in the listbox's line, so do whatever with them ...}
  16.       for Field in LineList do
  17.         {Do whatever}
  18.       (* ASerge just displayed them, but that won't work ia GUI app.
  19.         Writeln(Field);
  20.       Writeln('-------------');
  21.       *)
  22.     end;
  23.   finally
  24.     LineList.Free;
  25.   end;
  26. end;
  27.  

Yea, It's not an easy problem.

Huh? Au contrairé, it's a rather easy one!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: String Splicing?
« Reply #8 on: December 06, 2018, 02:22:11 am »
You can use unit LazStringUtils and split string to TStrings (TStringList):
Code: Pascal  [Select][+][-]
  1. SplitString('0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG', ';', ListBox1.Items, True);

But you will have to do it with each of 7500 records. IMO TListBox is not the right component here. Try TStringGrid and LoadFromCSVFile:
Code: Pascal  [Select][+][-]
  1. StringGrid1.LoadFromCSVFile('/home/v1/Documents/test.csv', ';', False, 0, True);
and you are done with one line.  :)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: String Splicing?
« Reply #9 on: December 06, 2018, 03:32:42 am »
Yea, It's not an easy problem.
It looks like you could use a good book on Pascal programming.   Without some understanding of the basics, you will find it difficult, not to mention frustrating, to get anything done.  If you're interested, pick up a copy of "Oh Pascal" by Doug Cooper, it's a really good book (basic programming, not object oriented, good, systematic analysis of problems and procedures so they can be represented in Pascal, very good place to start.)

Well Yea I'm Sure I could.

I'm 75 years old sitting  2 feet away from a 55" TV. On the 17th I go in for an eye operation, hopefully to improve my sight or come home with none.

I program for fun as I can't at the moment do much else except this and X-Plane 11 which this program is being developed for.

In the event my sight deteriorates more the program may make it easier to still fly the sim.

I will never program much better than I do right now. I have written a few programs in Free Pascal and received many answers from the Org.

Thanks for any help provided.

But I certinately can't read a book right now. Not even a good one.   
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: String Splicing?
« Reply #10 on: December 06, 2018, 03:43:09 am »
You can use unit LazStringUtils and split string to TStrings (TStringList):
Code: Pascal  [Select][+][-]
  1. SplitString('0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG', ';', ListBox1.Items, True);

But you will have to do it with each of 7500 records. IMO TListBox is not the right component here. Try TStringGrid and LoadFromCSVFile:
Code: Pascal  [Select][+][-]
  1. StringGrid1.LoadFromCSVFile('/home/v1/Documents/test.csv', ';', False, 0, True);
and you are done with one line.  :)

Say What!

If it works it's pretty elegant. I don't know if I know enough about Pascal to make this work. I'll try.

Have to create a new program. "StringGrid1.LoadFromCSVFile" have to google that. I guess the ';' specifies the field  delimiter.
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: String Splicing?
« Reply #11 on: December 06, 2018, 04:18:46 am »
On the 17th I go in for an eye operation, hopefully to improve my sight
I hope that will be the result.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: String Splicing?
« Reply #12 on: December 06, 2018, 04:34:37 am »
On the 17th I go in for an eye operation, hopefully to improve my sight
I hope that will be the result.

Yrs, Thanks

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018