Forum > Portuguese
Access the next occurrence of an argument in a TStringList
nightrider43:
Hi pals!
Suppose I created a list with TStringList and I used the IndexOf method to find an item in the list. Let's assume it was found in element 69 of the list.
If I want to search for new occurrences of the same search argument first found in element 69, how should I proceed?
Imagine that after item 69 already mentioned, the search argument occurs again in item 93 of the list. How do I query the next occurrence after the first one I found and so on?
Greetings
Ari Ricardo
Sao Paulo, Brazil
cdbc:
Hi
One way, would be like this:
--- 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";}};} ---type TScanResult = record srIdx: integer; srObj: TObject; srValue: string; end; TScanResultArray = array of TScanResult; function StringlistScanFor(aList: TStrings; aMask: string; aStrict: boolean = false): TScanResultArray; implementation function StringlistScanFor(aList: TStrings; aMask: string; aStrict: boolean): TScanResultArray;const szAlloc = 100;var acnt: integer = 0; i: integer; procedure AddToArray(var ra: TScanResultArray; anIdx: integer; aStr: string; anObj: TObject); begin if acnt = length(ra) then setlength(ra,length(ra)+szAlloc); ra[acnt].srIdx:= anIdx; ra[acnt].srObj:= anObj; ra[acnt].srValue:= aStr; inc(acnt); end; begin { main } if ((aList = nil) or (aMask = '')) then exit(nil); for i:= 0 to aList.Count-1 do begin case aStrict of false: if pos(aMask,aList[i]) > 0 then AddToArray(Result,i,aList[i],aList.Objects[i]); true: if aMask = aList[i] then AddToArray(Result,i,aList[i],aList.Objects[i]); end; end; SetLength(Result,acnt);end; Only quickly tested and it seems to work as expected :D
edit:
Oh, I tested like this:
--- 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";}};} ---var isl: IStringList; res: TScanResultArray; ... ... writeln('Testing StrLst Scan... Push enter'); readln; res:= StringlistScanFor(isl.List,'Benny',false); for i:= low(res) to high(res) do writeln(res[i].srIdx,': ',res[i].srValue); res:= []; res:= StringlistScanFor(isl.List,'# Bennys Lazarus account',true); for i:= low(res) to high(res) do writeln(res[i].srIdx,': ',res[i].srValue); res:= []; Where 'isl' is an IStringList and 'isl.List' is a TStringlist;
isl is loaded with a file containing some strings with my name in.
The result looks like this:
--- Quote ---4: Author: Benny Christensen /bc
47: # Bennys MitID
51: # Bennys Danske Bank konto
57: # Bennys Xiaomi Redmi 11 Pro 5G
78: # Bennys Lazarus account
82: # Bennys dropbox account primær
86: # Bennys dropbox sekundær
90: # Facebook "Benny Christensen"
136: # Bennys test konto hos google for udvikling
144: # Bennys Ebay konto
148: # Bennys Netflix account
153: login: BennyC123
164: # Bennys primære email
168: # Bennys sekundære email
172: # Bennys tertiære email
188: # Bennys login på red-devil
78: # Bennys Lazarus account
--- End quote ---
HTH 8-)
Regards Benny
ASerge:
--- Quote from: nightrider43 on June 06, 2024, 06:55:40 pm ---How do I query the next occurrence after the first one I found and so on?
--- 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}{$APPTYPE CONSOLE}{$LONGSTRINGS ON} uses Classes; var L: TStringList; Index: SizeInt;begin L := TStringList.Create; try L.CommaText := '1,2,3,2,4,5'; Index := L.IndexOf('2'); Writeln('First result: ', Index); if Index >= 0 then Index := TStrings(L).IndexOf('2', Index + 1); Writeln('Second result: ', Index); finally L.Free; end; Readln;end.
Remy Lebeau:
--- Quote from: ASerge on June 07, 2024, 01:39:27 am ---
--- 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";}};} ---Index := TStrings(L).IndexOf('2', Index + 1);
--- End quote ---
You shouldn't need the type-cast since TStringList inherits from TStrings and has access to all of the public TStrings methods:
--- 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";}};} ---Index := L.IndexOf('2', Index + 1);
ASerge:
--- Quote from: Remy Lebeau on June 07, 2024, 03:17:54 am ---You shouldn't need the type-cast since...
--- End quote ---
Should. TStringList overrides only one indexOf. Try it yourself.
Navigation
[0] Message Index
[#] Next page