I am an intermediate Delphi programmer that needs to learn a lot so I hope my question here is not to dumb. I have a file with 1546 strings that I need to place in a StringList and do a custom sort. The strings look like this:
2:X,X,2,2,2,X<A>11
7:5,7,7,6,5,5<A>08
3:3,X,0,0,1,0<C/D>11
5:X,2,4,2,5,2<Asus2/Gb>02
3:0,3,2,0,3,0<C/D>02
4:X,0,4,4,0,0<Asus2/Gb>11
4:X,X,4,4,4,2<B>01
3:3,2,1,0,0,3<B#5>11
I need them to look like this:
2:X,X,2,2,2,X<A>11
7:5,7,7,6,5,5<A>08
5:X,2,4,2,5,2<Asus2/Gb>11
4:X,0,4,4,0,0<Asus2/Gb>02
4:X,X,4,4,4,2<B>01
3:3,2,1,0,0,3<B#5>11
3:3,X,0,0,1,0<C/D>11
3:0,3,2,0,3,0<C/D>02
They need to be sorted by the portion of the string between the <...> and the last 2 chars. Any help would be much appreciated.
OK...done, Works quite well. Sorts a list with over 1500 strings in 62ms. Constructive criticism will be appreciated.
function SortChords(List:TStringList; idx1,idx2:integer): integer;
var
s1,s2:string;
begin
s1:=List[idx1];
s1:=copy(s1,pos('<',s1)+1,pos('>',s1)-pos('<',s1)-1);
s2:=List[idx2];
s2:=copy(s2,pos('<',s2)+1,pos('>',s2)-pos('<',s2)-1);
if s1 < s2 then
result:=-1
else if s1 > s2 then
result:=1
else
result:=0;
end;
You can write your own custom sort procedure and use TStringList.CustomSort to sort in the desired order.
The following demonstrates using the custom sort. It does not produce the exact output you describe, because you're not clear how you determine the precedence of two items that have the same value between the <> (as in lines 1 and 2, or 3 and 4, of your expected output; you can add code to decide the final order where I've indicated in the code comment. The sample is a complete console application that demonstrates sorting the values you've provided. It's slightly verbose in variable declarations for clarity.
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
function ListSortProc(List: TStringList; Index1, Index2: Integer): Integer;
var
StartPosA, EndPosA: Integer;
StartPosB, EndPosB: Integer;
TestValA, TestValB: string;
Comp: Integer;
begin
StartPosA := Pos('<', List[Index1]) + 1;
EndPosA := Pos('>', List[Index1]);
TestValA := Copy(List[Index1], StartPosA, EndPosA - StartPosA);
StartPosB := Pos('<', List[Index2]) + 1;
EndPosB := Pos('>', List[Index2]);
TestValB := Copy(List[Index2], StartPosB, EndPosB - StartPosB);
Result := CompareStr(TestValA, TestValB);
{ To do further processing for lines with the same value, add
code here.
if Result = 0 then
// Decide on the order of the equal values with whatever
// criteria you want.
}
end;
var
SL: TStringList;
s: String;
begin
SL := TStringList.Create;
try
SL.Add('2:X,X,2,2,2,X<A>11');
SL.Add('7:5,7,7,6,5,5<A>08');
SL.Add('3:3,X,0,0,1,0<C/D>11');
SL.Add('5:X,2,4,2,5,2<Asus2/Gb>02');
SL.Add('3:0,3,2,0,3,0<C/D>02');
SL.Add('4:X,0,4,4,0,0<Asus2/Gb>11');
SL.Add('4:X,X,4,4,4,2<B>01');
SL.Add('3:3,2,1,0,0,3<B#5>11');
SL.CustomSort(ListSortProc);
for s in SL do
WriteLn(s);
ReadLn;
finally
SL.Free;
end;
end.
The code above produces this output:
7:5,7,7,6,5,5<A>08
2:X,X,2,2,2,X<A>11
4:X,0,4,4,0,0<Asus2/Gb>11
5:X,2,4,2,5,2<Asus2/Gb>02
4:X,X,4,4,4,2<B>01
3:3,2,1,0,0,3<B#5>11
3:0,3,2,0,3,0<C/D>02
3:3,X,0,0,1,0<C/D>11
I have a memo lines like this:
Mahogany
Unpolished
In Stock : Yes
Total Stock : 102
Redwood
Polished
In Stock : Yes
Total Stock : 80
Pine
Polished
In Stock : Yes
Total Stock : 22
And i want to have only the line of Redwood's Total Stock.
Since there are many same string of
Total Stock
I can not use this string as my keyword. So, i use "Redwood" , but i don't know how to get the line of "Total Stock" after "Redwood".
var
i: Integer;
s: string;
begin
for i := 0 to mem0.lines.Count - 1 do
begin
if (AnsiContainsStr(mem0.lines[i], 'Redwood')) then
begin
// s:= Redwood's total stock, how to do this ?
end
end;
end;
The missing code is:
s := mem0.Lines[i+3];
This makes the assumption that the format of the data is always exactly as seen in the question. If that assumption is valid then this simple code is the best solution.
It would probably make more sense to use one of the standard human readable structured data formats like JSON or YAML that have good parsers and emitters. Sadly the support for YAML on Delphi is essentially non-existant so that leaves JSON.
You could try this, but like your original code it's a bit 'fragile' in that it makes assumptions about the layout of the text you're searching (in particular that the text you're searching through is packaged in a TStrings object):
function TotalForItem(const ItemName : String; Strings : TStrings) : String;
var
i,
j,
p : Integer;
s : string;
TotalLineIntro : String;
begin
Result := '';
TotalLineIntro := 'Total Stock : ';
for i := 0 to Strings.Count - 1 do
begin
if (Pos(ItemName, Strings[i]) > 0) then
begin
for j:= i + 1 to Strings.Count - 1 do begin
p := Pos(TotalLineIntro, Strings[j]);
if p > 0 then
begin
Result := Copy(Strings[j], p + Length(TotalLineIntro), Length(Strings[j]));
exit;
end;
end;
end
end;
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
ShowMessage(TotalForItem('Redwood', Memo1.Lines));
end;
Instead of AnsiContainsStr, you can use StartsWith, since the rest of the lines of the Redwood part are indentend.
var
I: Integer;
TotalStockLine: string;
MyArray: TArray<string>
begin
for I := 0 to Memo1.Lines.Count - 1 do
if Memo.Lines[I].StartsWith('Redwood') then
begin
TotalStockLine := Trim(Memo.Lines[I + 3];
Break;
end;
if TotalStockLine <> '' then
begin
MyArray := TotalStockLine.Split([':']);
TotalStock := StrToInt(Trim(MyArray[1]));
end;
etc... This can probably be simplified a little, but that shows how you could do this.
FWIW, this assumes you are using XE3 or later. Otherwise you must use the standalone StartsWith.
I want to add the files in the selected folder to the memobox or in a stringlist and show the results. In both ways, i can add them but i can't show the files from the folder in the memo or from the stringlist in a ShowMessage-dialog.
function CountFilesInFolder(AFolder: String; AMask: String): Integer;
var
tmp1: TSearchRec;
ergebnis: Integer;
memo1: string;
list : TStringList;
begin
result := 0;
if (AFolder <> '') then
begin
if AFolder[length(AFolder)] <> '\' then AFolder := AFolder + '\';
ergebnis := FindFirst(AFolder + AMask, faArchive + faReadOnly + faHidden + faSysFile, tmp1);
while ergebnis = 0 do
begin
Inc(result);
ergebnis := FindNext(tmp1);
while ((tmp1.Name = '|*_tif.tif')) and (ergebnis <> 0) do
ergebnis := FindNext(tmp1);
end;
list.Add(tmp1.Name);
FindClose(tmp1);
end;
end;
thank you for your time and sorry for my bad english.
A low-level function like this should not directly add items to a memo. Instead pass a TStrings (an abstraction of a string list) into the function and fill it:
function CountFilesInFolder(AFolder: String; AMask: String; FileNames: TStrings): Integer;
begin
// do your file enumeration
// for each file call FileNames.Add(FileName);
end;
Since the Lines property of a memo is also of type TStrings you can use it directly like this:
CountFilesInFolder('D:\', '*.TXT', Memo1.Lines);
If you wanted to have the filenames in a string list, the usual pattern goes like this:
FileNames := TStringList.Create;
try
CountFilesInFolder('D:\', '*.TXT', FileNames);
finally
FileNames.Free;
end;
The important point is that the caller creates and destroys the TStringList passed into CountFilesInFolder - an important principle in Delphi.
I have a list of strings and the values they are to be replaced with. I'm trying to combine them in a list like 'O'='0',' .'='.', ... so it's easy for me to edit it and add more pairs of replacements to make.
Right now the best way I could think of it is:
var
ListaLimpeza : TStringList;
begin
ListaLimpeza := TStringList.Create;
ListaLimpeza.Delimiter := '|';
ListaLimpeza.QuoteChar := '"';
ListaLimpeza.DelimitedText := 'O=0 | " .=."';
ShowMessage('1o Valor = '+ListaLimpeza.Names[1]+' e 2o Valor = '+ListaLimpeza.ValueFromIndex[1]);
This works, but it's not good for visuals, since I can't code the before string (for ex ' .') like that (which is very visual for the SPACE character), only like (" .) so that the = works to assign a name and value in the TStringList.
The Names and Values by default have to be separated by =, in the style of Windows INI files. There's no way AFAICT to change that separator. As #SirRufo indicates in the comment (and which I had never noticed), you can change that using the TStringList.NameValueSeparator property.
This will give you an idea of what Delphi thinks is in your TStringList, which is not what you think it is:
procedure TForm1.FormCreate(Sender: TObject);
var
SL: TStringList;
Temp: string;
i: Integer;
begin
SL := TStringList.Create;
SL.Delimiter := '|';
SL.QuoteChar := '"';
SL.StrictDelimiter := True;
SL.DelimitedText := 'O=0 | ! .!=!.!';
Temp := 'Count: ' + IntToStr(SL.Count) + #13;
for i := 0 to SL.Count - 1 do
Temp := Temp + Format('Name: %s Value: %s'#13,
[SL.Names[i], SL.ValueFromIndex[i]]);
ShowMessage(Temp);
end;
This produces this output:
TStringList Names/Values probably isn't going to do what you need. It's not clear what your actual goal is, but it appears that a simple text file with a simple list of text|replacement and plain parsing of that file would work, and you can easily use TStringList to read/write from that file, but I don't see any way to do the parsing easily except to do it yourself. You could use an array to store the pairs when you parse them:
type
TReplacePair = record
TextValue: string;
ReplaceValue: string;
end;
TReplacePairs = array of TReplacePair;
function GetReplacementPairs: TReplacePairs;
var
ConfigInfo: TStringList;
i, Split: Integer;
begin
ConfigInfo := TStringList.Create;
try
ConfigInfo.LoadFromFile('ReplacementPairs.txt');
SetLength(Result, ConfigInfo.Count);
for i := 0 to ConfigInfo.Count - 1 do
begin
Split := Pos('|`, ConfigInfo[i];
Result[i].TextValue := Copy(ConfigInfo[i], 1, Split - 1);
Result[i].ReplaceValue := Copy(ConfigInfo[i], Split + 1, MaxInt);
end;
finally
ConfigInfo.Free;
end;
end;
You can then populate whatever controls you need to edit/add/delete the replacement pairs, and just reverse the read operation to write them back out to save.
Lets say I have a String: Go to this page: http://mysite.com/?page=1 , and I have a string page. I would like to create a function like so:
MyBoolean := IsLink('Go to this page: http://mysite.com/?page=1','page',sLink);
// sLink is a Var, so it would return http://mysite.com/?page=1
Basically it is supposed to check if the word "page" is part of a link or not.
However I just can't figure it out. Any tips?
You could do something like
function GetLinkContaining(const Str, SubStr: string; out URL: string): boolean;
const
ValidURLSpecialChars = ['.', ':', '/', '?', '=', '&', '%'];
Prefixes: array[0..4] of string = ('http://', 'https://', 'ftp://', 'mailto:',
'www.');
function IsValidURLChar(const Char: char): boolean;
begin
result := IsLetterOrDigit(Char) or (Char in ValidURLSpecialChars);
end;
var
SubStrPos: integer;
Start, &End: integer;
i: Integer;
URLBegin: integer;
begin
result := false;
URLBegin := 0;
for i := low(Prefixes) to High(Prefixes) do
begin
URLBegin := Pos(Prefixes[i], Str);
if URLBegin > 0 then
break;
end;
if URLBegin = 0 then Exit(false);
SubStrPos := PosEx(SubStr, Str, URLBegin);
if SubStrPos = 0 then Exit(false);
Start := SubStrPos;
for i := SubStrPos - 1 downto 1 do
if IsValidURLChar(Str[i]) then
dec(Start)
else
break;
&End := SubStrPos + length(SubStr);
for i := SubStrPos + length(SubStr) to length(Str) do
if IsValidURLChar(Str[i]) then
inc(&End)
else
break;
URL := Copy(Str, Start, &End - Start);
result := true;
end;
To test it:
procedure TForm1.FormCreate(Sender: TObject);
var
s: string;
begin
if GetLinkContaining('Go to this page: http://mysite.com/?page=1 (right now!)',
'page', s) then
ShowMessage(s);
if GetLinkContaining('This is my favourite site (www.bbc.co.uk).', 'bbc', s) then
ShowMessage(s);
end;
To check if 'page' is part of a string you can use the function Pos.
function Pos(Str, Source : string): integer;
Pos returns an integer specifying the position of the first occurrence of one string within another.
Pos looks for the first complete occurence of Str in Source. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise it returns 0. Pos is case sensitive. So mybe you have to deal with upper- and lowe-case situations.
To extracrt the URL is (maybe) not so easy, you have to define more conditions. If the URL is always at the end of your string, you can copy everything from the http on (also use Pos and Copy!)
Among the more powerful string matching algorithms there are regular expressions. They allow for very complex matches without writing much code, although mastering them may require a little time. Latest versions of Delphi have already regular expression libraries, but you can find some for earlier versions as well.