Here is why I asked. lazarus / 61-Beginners ---------------------------------------------------------------------- JLWest · 2018-02-15 21:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [code=pascal]Example 1:         10        20        30        40      50        60                  70    123456789012345678901234567890123 45678901234567890123456789012345678901234567890*Boeing767-300ER                  \P3d            \plugins\757767Avionics\routes\-Aero- Commander/500S         XXX    \X-Plane*B737-NG/P3D                 fltplan\P3d            \iFly\737NG\navdata\FLTPLAN\*B737-NG/FSX               fltplan\FSX            \iFly\737NG\navdata\FLTPLAN\-Embraer-140/Regional-JetERJ KLM    \smUnknown*B767-300ER/FF               flp    \X-Plane        \Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\ Example 2: 1 2345678901234567890123456789012345678901234567890123456789012345678901 234567890*Boeing767-300ER       \P3d            \plugins\757767Avionics\routes\-Aero-Commander/500S XXX    \X-Plane*B737-NG/P3D fltplan\P3d            \iFly\737NG\navdata\FLTPLAN\*B737-NG/FSX            fltplan\FSX          \iFly\737NG\navdata\FLTPLAN\-Embraer-140/Regional-JetERJ KLM    \smUnknown*B767-300ER/FF      flp \X-Plane\Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\  I have a text file of the following strings I need to process. What is important is the following: Column 1                          '- or *' Column 30 -36                  Could be 3 to 7 characters Column 37 - 52                 Always starts with '\' and ends with '\' Column 53 to end of line   Always starts with a '\' and ends with '\' Wit 1 or more '\' in between The data in the file is not aligned as shown in Example 1. It looks more like Example 2. I know how to get the first "- or *" with a copy(S,1,1). Lgth := Length(LineText) gives me the length of the entire string. There is never a space in the first entry (-Aero-Commander/500S) so I can extract that with a copy2 space. Now I'm at a loss. Would I have to write function to give me the location of the first '\' second '\' or every position of '\' in the string. ---------------------------------------------------------------------- taazz · 2018-02-15 22:09 Are those static sizes? if yes why not use MidStr https://www.freepascal.org/docs-html/rtl/strutils/midstr.html ? something along the lines of Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  vLine, vFld1,vFld2,vFld3,vFld4,vFld5,vFld6:string;begin  vline := readline;  vfld1 := midstr(vLine,1,1);  vfld2 := midstr(vLine,2,28);  vfld3 := midstr(vLine,30,6);  vfld4 := midstr(vLine,37,15);  vfld5 := midstr(vLine,53,length(vLine)); //it will return everything after the 53rd position;  //  //handle the data in the fields and read the next line.end; ---------------------------------------------------------------------- JLWest · 2018-02-15 22:16 No they are not static sizes. ---------------------------------------------------------------------- molly · 2018-02-15 22:41 then use extractword. Not sure if this is 100% working for you but should be able to give you an idea: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; {$MODE OBJFPC}{$H+} uses  Classes, StrUtils;   procedure Action(FileName: string);var  Lines: TStringList;  Line : String;  n, i : integer;  Part1, part2, part3: string;  EntryCount: integer = 0;begin  Lines := TStringList.Create;  Lines.LoadFromFile(Filename);   for line in lines do  begin    if Line <> '' then    begin      n := WordCount(Line, [' ']);      WriteLn('Found ', n, ' fields in entry ', EntryCount);       Part1 := ExtractWord(1, Line, [' ']);      Part2 := ExtractWord(2, Line, [' ']);      Part3 := ExtractWord(3, Line, [' ']);       WriteLn('part 1 = ', Part1);      WriteLn('part 2 = ', Part2);      WriteLn('part 3 = ', Part3);    end    else inc(EntryCount);    end;    Lines.Free;  end;  begin  Action('data.txt');end.  data.txt: Code: [Select]*Boeing767-300ER                    \P3d            \plugins\757767Avionics\routes\ -Aero-Commander/500S         XXX    \X-Plane *B737-NG/P3D                 fltplan\P3d            \iFly\737NG\navdata\FLTPLAN\ *B737-NG/FSX                 fltplan\FSX            \iFly\737NG\navdata\FLTPLAN\ -Embraer-140/Regional-JetERJ KLM    \smUnknown *B767-300ER/FF               flp    \X-Plane        \Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\ *Boeing767-300ER       \P3d            \plugins\757767Avionics\routes\ -Aero-Commander/500S XXX    \X-Plane *B737-NG/P3D fltplan\P3d            \iFly\737NG\navdata\FLTPLAN\ *B737-NG/FSX            fltplan\FSX            \iFly\737NG\navdata\FLTPLAN\ -Embraer-140/Regional-JetERJ KLM    \smUnknown *B767-300ER/FF      flp \X-Plane\Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\ edit: fixed dumb error in my code ---------------------------------------------------------------------- dbannon · 2018-02-15 22:46 I think your example 1 and the notes about its columns are a little misleading. So example 2 is really how the data looks ?  Assuming yes - There is always whitespace between fields ? Some fieids might be empty ? The first field is never empty ? Is the whitespace spaces or tabs ? you could set a pointer to the start of each string and then inc it looking to see if its whitespace and make decisions about start and end of each data. Just a few lines and will take  bit of fiddling to get it right. Yeah, I know, its a C way of doing it. TStrngList will break a string up into component parts, it uses the newline as delimiter by default but you can set the delimitator to, eg a space. I don't  know if it will react badly to multiple spaces, but thats a much more object pascal approach, something like -      Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         SL := TStringList.Create;        SL.LineBreak:=' ';                  SL.AddText(trim(OneString));        for I := 0 to SL.Count -1 do              debugln(SL.Strings[I])        SL.Free;        SL := Nil;You will need to look at first char in SL.strings[1] to see if it starts with '\' to determine what sort of data it is... ---------------------------------------------------------------------- dbannon · 2018-02-15 22:54 and Molly said - Quotethen use extractword. ... Hmm, thats a useful function. Its not mentioned on the wiki, its a rtl function. Molly, do you know if its UTF8 compatible ? So many lovely string functions, so little documentation ..... David ---------------------------------------------------------------------- molly · 2018-02-15 23:01 I'm not sure if strutils is 100% unicode proof. Indeed to many string functions to choose from. Using the stringlist the weay you did should help as well. The only misleading part imho is the last line in the first example: "*B767-300ER/FF               flp    \X-Plane        \Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\" which has 4 fields.... if that really is to be the case then wordcount will show 4 instead of 3 and code should act accordingly. That is why i'm not sure my code is 100% working for OP. ---------------------------------------------------------------------- rvk · 2018-02-15 23:07 Quote from: JLWest on February 15, 2018, 10:42:16 pmI have a text file of the following strings I need to process. Maybe it's useful to mention where these text files come from. Are these X-Plane configuration or flightplan files? There is documentation on those: https://developer.x-plane.com/docs/specs/ ---------------------------------------------------------------------- JLWest · 2018-02-15 23:46 "Maybe it's useful to mention where these text files come from." It's for an utility for X-Plane and they come from a file I built. The data in the .txt file is perfectly lined up. But when I load it into a string or listbox it doesn't line up. So I'm loading it into an array of strings. "The only misleading part imho is the last line in the first example:" Your probably right. The only misleading part imho is the last line in the first example: "*B767-300ER/FF               flp    \X-Plane        \Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\" which has 4 fields.... if that really is to be the case then wordcount will show 4 instead of 3 and code should act accordingly. That is why i'm not sure my code is 100% working for OP. There can as many as 4 and but will always be at least one. '*Aircraft' is actually 2 fields but because '*' it's the first char it's easy and could be treated as 1 field. ---------------------------------------------------------------------- jamie · 2018-02-16 00:02 maybe TAB characters are getting in there ? also, you could make a Parser that reads the line using a space as the delimiter  the parser would start building a string until it encounters a space, the parser will then test to see if the accumulated string has any characters in it at this point, if no, then the parser will continue skipping the spaces and not until  it finds a real character to put in the accumulator string will the parser stop and return your accumulated string..  The idea is to keep a reference index for the parser so that when you get ready to fetch the next  group of characters it knows where to start...  By doing this, you can there for have staggered entries using spaces as the separator..  if you have issues understanding this concept I or someone else here most likely can provide an example of a parser that uses space as the delimiter ---------------------------------------------------------------------- JLWest · 2018-02-16 00:08 I think I have enough info to solve the problem. Tabs in the input. I think that might be the problem. I'll check that first. Thanks to all. ---------------------------------------------------------------------- molly · 2018-02-16 00:26 Quote from: JLWest on February 16, 2018, 12:46:39 amIt's for an utility for X-Plane and they come from a file I built. In case you are the one creating those text-files/lines for input, then by all means select a proper format to distinguish between different fields. The tab character is the best character around in that regards as a GUI usually respond to a tab as is you where changing/switching between gui elements, ergo it is difficult for an end-user to input a tab character (copy-paste still allows it though). But, you can use any character, or use some form of quotes around each individual field. QuoteThe data in the .txt file is perfectly lined up. But when I load it into a string or listbox it doesn't line up. Font problem ? GUI elements usually use proportional fonts, which causes characters to line up differently then when using a mono-spaced font. QuoteThere can as many as 4 and but will always be at least one. If you select your separator characters carefully then you are always able to distinguish between the the different storage methods and a function like wordcount (or something similar) is always able to tell you how many fields there actually are. Don't make the mistake to just leave out empty fields without proper separator because in that case you'll never know which exact field is missing from your data (unless you add an additional field that explains what data is stored into a single line). PS: there are far better methods for storing data as you showed. Even a inifile will do better. But xml, json or using a csv or sqlite table will be able to do a better job that makes things much easier for your program to handle (even though it perhaps looks like a lot more work). It depends a little on what your program actually does, as your program allows for editing the files manually and therefor allows for mistakes. My suggestion could be overkill for what your program is trying to achieve/offer though, but at this point in time i wouldn't know ---------------------------------------------------------------------- JLWest · 2018-02-16 00:59 @Molly "Even a inifile will do better." I agree but I can't find a simple ini file reader to look at to understand ini file processing. ---------------------------------------------------------------------- taazz · 2018-02-16 01:23 Quote from: JLWest on February 16, 2018, 01:59:00 am@Molly "Even a inifile will do better." I agree but I can't find a simple ini file reader to look at to understand ini file processing. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses inifile;Procedure ReadData(const aFilename:String);var  vIni :TInifile;  vModel:String;begin  vIni := tIniFile.Create(aFilename);  try    vmodel := vIni.ReadString('Plane','Model','Unknown model');    vFromAirport := vIni.ReadString('Plane','From','Unknown starting point');    vToAirport := vIni.ReadString('Plane','To','Unknown destination');    vRoute := vini.ReadString('plane','Route','Unknown Route');  finally    vIni.free;  end;end;  the above is sample to read a single plane data from an inifile to write an ini file you simple change the readstring to writestring. 'Plane' is the section name, it will be seen in the file as [Plane] 'Model' is the parameter name it will be seen in the file as Model=, every parameter occupies a line in the file so the above sample will expect an ini file of the format Code: [Select][Plane] Model = 747 From = HNL To = MKK or what ever parameters you need. TInifile has multiple read and write methods for different data types to help you with the conversion from and to string read the help for them. ---------------------------------------------------------------------- molly · 2018-02-16 01:32 @JLWest: could you share a bit more about descriptions on each individual field ? You said that even the first files can exist of multiple parts, and i'm after all possible (individual) fields that can exist. "*B767-300ER/FF               flp    \X-Plane        \Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\" status=* ???=B767-300ER ???=FF ???=flp ???=\X-Plane ???=\Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\ or should some of the above fields even be split into more individual fields ? i want to know what to use for the ??? question marks to name the fields. TInifile, mre methods come from TCustomIniFile Depending on your needs: Code: [Select][example1] lines=10 [example1.line.1] status=* ???=B767-300ER ???=FF ???=flp ???=\X-Plane ???=\Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\ [example1.line.2] etc Fields that have no data can be left out as TIniFile methods allows for you to detect that those entries do not exist (even though the code reading the .ini tries to load them but detect the filed is not there and empties corresponding field of your internal storage). In case it suits better, TMemInifile. You can also use the ini as internal storage in case that is easier. A small (entry-)wrapper would allow for easier access in such case. ---------------------------------------------------------------------- jamie · 2018-02-16 01:52 Here is something I Just whipped up for a simple line parser.. Educational purposes only  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Const TheInputStr:String = '*Boeing767-300ER       \P3d          \plugins\757767Avionics\routes\';var  WordCount,I :Integer;  Entries :Array[1..4] of string;Function GetParserWord(Var parserindex:Integer; InStr:String):String; begin   Result := '';   // First remove spaces.   While (ParserIndex <= Length(InStr))and(InStr[ParserIndex] in [#8,' '])    do Inc(ParserIndex);   While (ParserIndex <= Length(INStr))and (Not (InStr[ParserIndex] in[#8,' ']))    do     Begin      Result := Result+Instr[ParserIndex];      Inc(ParserIndex);     End; End;Function BuildWordArray(InStr:String):Integer;Var  I:Integer;Begin  For I := Low(Entries) to High(Entries) do Entries[I] := '';  Result := 1;  I := 1;  Repeat   Entries[Result] := GetParserWord(I,Instr);   Inc(Result);  until (Entries[Result-1] = '') or(Result > High(Entries));  Result := Result-1;end; begin WordCount := BuildWordArray(TheInputStr); For I := 1 To WordCount do  WriteLn(Entries[I]);  ReadLn;end.