[SOLVED] Creating separate lists from a larger one using a grouping variable lazarus / 21-General ---------------------------------------------------------------------- maurobio · 2021-02-21 18:35 Hi! I have been stuck with the following problem: I have a comma-separated values file in the following format: 1,Name A 2,Name B 2,Name C 3,Name D 4,Name E 4,Name F where the first column is a grouping variable. I want to read this file into a stringlist, then iterate over the lines and get each group of lines into separate lists; so, for example, I would have a list with the lines in group 1, another one with the lines in group 2, and so on. Here is may lame attempt to code a solution so far: 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; {$APPTYPE CONSOLE}{$mode objfpc}{$H+} uses    Classes, SysUtils, StrUtils; var        SList: TStringList;        i: integer; begin       SList := TStringList.Create;        SList.Sorted := True;        SList.Duplicates := dupIgnore; (* the list can have duplicates; they are to be ignored *)        SList.LoadFromFile('test.csv');        (* iterate over the stringlist and handle each line *)       for i := 0 to SList.Count - 1 do       begin           (* here is where the contents of the stringlist should be handled *)       end;        SList.Free;         end. Could someone give me an idea? With best wishes, ---------------------------------------------------------------------- maurobio · 2021-02-21 20:56 @jamie, Thanks a lot for your insightful comments. I will try and use them to advance a solution. Quotefrom there you scan your main list and resolve the first integer value as the group index and then add it to the group.. ...but that is, I regret, my main problem! With best wishes, ---------------------------------------------------------------------- maurobio · 2021-02-22 10:40 @jamie, Thanks a lot, your solution works fine!  With best regards, ---------------------------------------------------------------------- engkin · 2021-02-22 18:09 I got bored as well. Here is four methods to read and parse a CSV file Using LoadFromCSVFile from unit lcvutils: 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 //needs {$modeswitch nestedprocvars}procedure Using_unit_lcvutils_LoadFromCSVFile;  procedure YourProc(Fields: TStringList);  var    sNum, sName: String;  begin    sNum := Fields[0];    sName := Fields[1];  end;begin  LoadFromCSVFile(CSVFileName, @YourProc);end; Using TCSVParser from unit csvreadwrite: 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 procedure Using_unit_csvreadwrite_TCSVParser;var  parser: TCSVParser;  stream: TFileStream;  sNum, sName: String;begin  parser := TCSVParser.Create;  try    stream := TFileStream.Create(CSVFileName, fmOpenRead);    parser.SetSource(stream);    while parser.ParseNextCell do begin      sNum := parser.CurrentCellText;// parser.CurrentCol is 0      sName := parser.CurrentCellText;// parser.CurrentCol is 1    end;  finally    stream.Free;    parser.Free;  end;end; Using TCSVDocument from unit csvdocument: 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 procedure Using_unit_csvdocument_TCSVDocument;var  doc: TCSVDocument;  r: Integer;  sNum, sName: String;begin  doc := TCSVDocument.Create;  try    doc.LoadFromFile(CSVFileName);    for r := 0 to doc.RowCount-1 do begin      sNum := doc.Cells[0,r];      sName := doc.Cells[1,r];    end;  finally    doc.Free;  end;end; Using TCSVDataset from unit csvdataset: 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 //There is a non-visual component in "Data Access" pal.procedure Using_unit_csvdataset_TCSVDataset;var  ds: TCSVDataset;  sNum, sName: String;begin  ds := TCSVDataset.Create(nil);  try    ds.LoadFromCSVFile(CSVFileName);    ds.Active:=True;    while not ds.EOF do    begin      sNum := ds.Fields[0].AsString;      sName := ds.Fields[1].AsString      ds.Next;    end;  finally    ds.Free;  end;end; ---------------------------------------------------------------------- maurobio · 2021-02-22 18:23 @engkin, Thanks for the code. My problem was not parsing the csv file, but dividing it into separate groups in-memory according to a grouping variable. With best wishes,