Recent

Author Topic: String Splicing?  (Read 292 times)

JLWest

  • Full Member
  • ***
  • Posts: 225
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
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

ASerge

  • Hero Member
  • *****
  • Posts: 998
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

  • Full Member
  • ***
  • Posts: 225
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?
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

dsiders

  • Jr. Member
  • **
  • Posts: 62
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.
Lazarus 1.8.2 / FPC 3.0.4 / Windows 8.1 64-bit

JLWest

  • Full Member
  • ***
  • Posts: 225
Re: String Splicing?
« Reply #4 on: December 06, 2018, 01:49:28 am »
Yea, It's not an easy problem.
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

dsiders

  • Jr. Member
  • **
  • Posts: 62
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.
Lazarus 1.8.2 / FPC 3.0.4 / Windows 8.1 64-bit

440bx

  • Sr. Member
  • ****
  • Posts: 387
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.)

lucamar

  • Sr. Member
  • ****
  • Posts: 496
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 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 11..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

Blaazen

  • Hero Member
  • *****
  • Posts: 2676
  • 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.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
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

  • Full Member
  • ***
  • Posts: 225
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.   
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

JLWest

  • Full Member
  • ***
  • Posts: 225
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.
 
JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD

440bx

  • Sr. Member
  • ****
  • Posts: 387
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.

JLWest

  • Full Member
  • ***
  • Posts: 225
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

JLWEST 
 FPC 3.0.4, Lazarus IDE v1.8.2 Windows 10 Pro
 Intel i7 770K CPU 4.2GHz 16 Gig Ram
GeForce GTX 1080 Graphics - 8 Gig 1.5 Terabyte SSD