Forum > Beginners

[SOLVED] Comparing few TStringLists

(1/1)

TomTom:
 :-[ This is one of "those embarrassing" problems. I know it's simple, but somehow I can't figure it out. Here's what I have
I have such an N element array of TStringLists. I don't know how many of them I'll have, since they will ultimately be lists of file paths from multiple folders, but for testing purposes I made a simpler example.
So I have such an array of TStringList. For now, let's assume that there are 3 StringLists in this array.
What I need to do is compare each element (well part of it, part after _ underscore, but for now I want to compare/display each comparison) of each StringList (even with the elements of themselves) with the elements in the other lists and find duplicates.
So what I'm thinking I'm gonna need, is that I'll have to use few (3?) nested FOR loops. But I can't figure out how to set them up. Sometimes visualization helps me in such a problem, but this time I somehow can't visualize the problem :P

So in my example
Compare
0_1 with 0_1, 0_2, 0_3, 0_4, 0_5, 1_5, 1_6, 1_7, 1_8, 2_5, 2_6, 2_2, 2_8, and so on..
0_2 with 0_1, 0_2, 0_3, 0_4, 0_5, 1_5, 1_6, 1_7, 1_8, 2_5, 2_6, 2_2, 2_8, and so on..
...
1_5 with 0_1, 0_2, 0_3, 0_4, 0_5, 1_5, 1_6, 1_7, 1_8, 2_5, 2_6, 2_2, 2_8, and so on..
1_6 with 0_1, 0_2, 0_3, 0_4, 0_5, 1_5, 1_6, 1_7, 1_8, 2_5, 2_6, 2_2, 2_8, and so on..
....
etc.

For now I don't care if im comparing elements few times.
 :-[


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  tab : array of TStringList;  i, j, k: Integer;  sstr: String;begin  SetLength(tab,2);  tab[0] := TStringList.Create;  tab[1] := TStringList.Create;  tab[2] := TStringList.Create;  tab[0].Sorted:=true;  tab[1].Sorted:=true;  tab[2].Sorted:=true;   tab[0].add('0_1');  tab[0].add('0_2');  tab[0].add('0_3');  tab[0].add('0_4');  tab[0].add('0_5');   tab[1].add('1_5');  tab[1].add('1_6');  tab[1].add('1_7');  tab[1].add('1_8');  tab[2].add('2_5');  tab[2].add('2_6');  tab[2].add('2_2');  tab[2].add('2_8');     for i:=0 to Length(tab) do  Begin    for j:= 0 to tab[i].Count-1 do    Begin     //  sstr := copy(tab[i][j],2,9);       for k:=0 to tab[i].count -1 do       Begin          Memo1.lines.add('Comparing '+ tab[i][j]+' with '+tab[i][k]);       end;    end;   end;   end;

ASerge:

--- Quote from: TomTom on July 27, 2022, 06:44:05 pm ---What I need to do is compare each element (well part of it, part after _ underscore, but for now I want to compare/display each comparison) of each StringList (even with the elements of themselves) with the elements in the other lists and find duplicates.

--- End quote ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$LONGSTRINGS ON}{$APPTYPE CONSOLE} uses SysUtils, Classes; type  TMyCompareList = class(TStringList)  protected    function DoCompareText(const S1, S2: string): PtrInt; override;  end; function TMyCompareList.DoCompareText(const S1, S2: string): PtrInt;var  P1, P2: SizeInt;begin  P1 := Pos('_', S1);  P2 := Pos('_', S2);  Result := CompareStr(    Copy(S1, P1 + 1, MaxInt),    Copy(S2, P2 + 1, MaxInt));end; procedure Test;const  CExternalData = '0_1 0_2 0_3 0_4 0_5 1_5 2_3';var  Source, DupFinder: TStringList;  Index: Integer;  S: string;begin  Source := TStringList.Create;  try    Source.CommaText := CExternalData;    DupFinder := TMyCompareList.Create;    try      DupFinder.SortStyle := sslUser; // Sort, but skip Insert error      for S in Source do      begin        if DupFinder.Find(S, Index) then          Writeln('Item ', S, ' is duplicate of ', DupFinder[Index])        else          DupFinder.Insert(Index, S);      end;    finally      DupFinder.Free;    end;  finally    Source.Free;  end;end; begin  Test;  Readln;end.

TomTom:
Although I won't use Your solution it made me commit JUICY face palm :)  %)
I have tendency to overcomplicate simple tasks. OFCOURSE I can use one TStringList for this (well and second one for result of processing first one).
Just two nested FOR loops will do for me :).


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin   a:=TStringlist.create;a.add('X:\tomjab\2022\ZielonaGora\18637-18780\89\225\0\2\170\');a.add('X:\tomjab\2022\ZielonaGora\18637-18780\89\225\0\2\171\');a.add('X:\tomjab\2022\ZielonaGora\18637-18780\89\225\0\2\172\');a.add('X:\tomjab\2022\ZielonaGora\18637-18781\89\960\0\1\1\');a.add('X:\tomjab\2022\ZielonaGora\18637-18781\89\960\0\1\2\');a.add('X:\tomjab\2022\ZielonaGora\18637-18781\89\225\0\2\171\');a.add('X:\tomjab\2022\ZielonaGora\18637-18783\89\960\0\1\2\');a.add('X:\tomjab\2022\ZielonaGora\18637-18784\89\963\0\1\2\');a.add('X:\tomjab\2022\ZielonaGora\18637-18785\89\960\0\1\2\');  end; procedure TForm1.Button1Click(Sender: TObject);var  i, j: Integer;  syg, ser, cd, zesp, arch, st: String;  r: TStringList;begin     r:=TStringList.Create;     r.sorted:=true;     r.duplicates:=dupignore;   for i:=0 to a.Count-1 do  Begin    syg  := ReverseString(ExtractDelimited(2,ReverseString(a[i]),['\'])); // 1st folder from the end    ser  := ReverseString(ExtractDelimited(3,ReverseString(a[i]),['\'])); // 2nd folder from the end    cd   := ReverseString(ExtractDelimited(4,ReverseString(a[i]),['\'])); // 3rd folder from the end    zesp := ReverseString(ExtractDelimited(5,ReverseString(a[i]),['\'])); // 4th folder from the end    arch := ReverseString(ExtractDelimited(6,ReverseString(a[i]),['\'])); // 5th folder from the end    st:=arch+'\'+zesp+'\'+cd+'\'+ser+'\'+syg; //whole main folder structure string    for j:=0 to a.count-1 do        Begin              if i<>j then              begin                   if AnsiContainsText(a[j],st) then                      Begin                           r.add(a[j]);                      end;              end;        end;  end;  memo1.lines.add('===============');  for i:=0 to r.count-1 do  begin    memo1.lines.add(r[i]);  end; end;

--- Quote from: ASerge on July 27, 2022, 07:38:53 pm ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$LONGSTRINGS ON}{$APPTYPE CONSOLE} uses SysUtils, Classes; type  TMyCompareList = class(TStringList)  protected    function DoCompareText(const S1, S2: string): PtrInt; override;  end; function TMyCompareList.DoCompareText(const S1, S2: string): PtrInt;var  P1, P2: SizeInt;begin  P1 := Pos('_', S1);  P2 := Pos('_', S2);  Result := CompareStr(    Copy(S1, P1 + 1, MaxInt),    Copy(S2, P2 + 1, MaxInt));end; procedure Test;const  CExternalData = '0_1 0_2 0_3 0_4 0_5 1_5 2_3';var  Source, DupFinder: TStringList;  Index: Integer;  S: string;begin  Source := TStringList.Create;  try    Source.CommaText := CExternalData;    DupFinder := TMyCompareList.Create;    try      DupFinder.SortStyle := sslUser; // Sort, but skip Insert error      for S in Source do      begin        if DupFinder.Find(S, Index) then          Writeln('Item ', S, ' is duplicate of ', DupFinder[Index])        else          DupFinder.Insert(Index, S);      end;    finally      DupFinder.Free;    end;  finally    Source.Free;  end;end; begin  Test;  Readln;end.
--- End quote ---

Navigation

[0] Message Index

Go to full version