Delete , Add, Edit stringList - delphi

This is a bit confusing but will try best to explain it. please ask if you need more details.
First i have a class called TPlayers Like so..
TPlayers = class
Private
p : array[1..20] of TStringList;
function GetPlayer(i:integer): TStringList;
Public
Property player[i : integer] : TStringList read GetPlayer;
constructor Create; virtual;
implementation
uses
main;
{constructor}
constructor TPlayers.Create;
begin
p[1] := TStringList.Create;
p[2] := TStringList.Create;
p[3] := TStringList.Create;
p[4] := TStringList.Create;
p[5] := TStringList.Create;
p[6] := TStringList.Create;
end;
function TPlayers.GetPlayer(i: integer): TStringList;
begin
Result := p[i];
end;
I now have FTherePlayers := TPlayers.Create to create the class.
First time i add to the stringlist like so
FTherePlayers.Player[strtoint(name2)].Add('posx='+inttostr(posL.x));
or with variables taken out
FTherePlayers.Player[1].Add('posx=15');
This seems to be fine, but next i try to update it like so
FTherePlayers.Player[strtoint(ID)].Values['posx='] := xpos;
or with variables taken out
FTherePlayers.Player[1].Values['posx='] := 12;
but then i check that value after changing it and it still says 15, thus when i do
showmessage(fthereplayers.player[1].Values['posx']);
it returns 15 but it should be 12. Any idea why its not changeing?
thanks
Glen

You have an extra equals sign at the end of the Name index value of the Values property. You need to use only the name portion of a name value pair without the equals sign. So, in your code just replace the following lines:
// here is an extra equals sign in 'posx=' index value
FTherePlayers.Player[1].Values['posx='] := 12;
FTherePlayers.Player[strtoint(ID)].Values['posx='] := xpos;
with this:
FTherePlayers.Player[1].Values['posx'] := 12;
FTherePlayers.Player[strtoint(ID)].Values['posx'] := xpos;

Related

Stringlist with CommaText is returning comma and spaces instead of just comma in Delphi

I have a string list, each string looks something like this.
2023/01/30,08:47:27, 0. 7.71,CM212-A2,03,Bad head/nozzle detect,380000,Stage No1, Head No2, Nozzle Postion5, Nozzle No0, (Single Lane), PCB ID: , Bad head/nozzle detectReset
I am then adding that string into another list with commatext, and should be giveing a result like so
2023/01/30
08:47:27
0. 7.71
CM212-A2
03
Bad head/nozzle detect
Instead it returns
2023/01/30
08:47:27
0.
7.71
CM212-A2
03
Bad
head/nozzle
detect
It seems it will create a new string for each Comma like I want, but also is creating a new string for each space which I don't want.
Adding Code just incase its needed.
procedure TForm4.Button1Click(Sender: TObject);
var
datalist : tstringlist;
eventlist : tstringlist;
i,x: Integer;
filename : string;
Event : array[1..200] of TEvent;
begin
datalist := tstringlist.Create;
eventlist := tstringlist.Create;
X := 1;
//get latest file
filename := getlastmodifiedfilename('C:\Users\tngmorse\Desktop\LockHeads\Win32\Debug\');
//end
datalist.LoadFromFile(filename);
//search for head lockout
for i := 0 to datalist.count - 1 do
begin
if containstext(datalist[i],'Bad head') then
begin
memo1.Lines.Add(datalist[i]);
// eventlist.Clear;
eventlist.CommaText := datalist[i];
// Event[x].Dateofevent := 'today';
Event[x].Dateofevent := eventlist[0]; //spaces are also breaking it.
Event[x].Timeofevent := eventlist[1];
Event[x].Version := eventlist[2];
Event[x].Machine := Eventlist[3];
Event[x].EventNumber := Eventlist[4];
Event[x].EventName := Eventlist[5];
Event[x].TableNumber := EventList[6];
Event[x].Table := Eventlist[7];
Event[x].Head := Eventlist[8];
Event[x].Nozzle := EventList[9];
Event[x].NozzleType := Eventlist[10];
Event[x].Lane := Eventlist[11];
Event[x].PCBID := Eventlist[12];
Event[x].Effect := Eventlist[13];
memo1.Lines.Add('Date: '+Event[x].Dateofevent);
memo1.Lines.Add('Time: '+Event[x].Timeofevent);
memo1.Lines.Add('Machine: '+Event[x].Machine);
memo1.Lines.Add('Event: '+Event[x].EventName);
memo1.Lines.Add('Table: '+Event[x].Table);
memo1.Lines.Add('Head: '+Event[x].Head);
memo1.Lines.Add('Nozzle: '+Event[x].Nozzle);
memo1.Lines.Add('Status: '+Event[x].Effect);
x:=x+1;
//break down list by comma
//add to record
end;
end;
//end
showmessage('lines: '+inttostr(datalist.Count));
datalist.Destroy;
end;
This behavior can be controlled by the StrictDelimiter property:
If StrictDelimiter is True, individual strings in DelimitedText are only separated by Delimiter or quoted between QuoteChar. If StrictDelimiter is False, spaces and non-printable character are also used as delimiters.
So, set it to True to get the desired behavior.

How to extract the first instance of unique strings

I need to extract a list of unique items from 12 years' worth of consistent computer-generated one-per day text files. The filenames vary only by the included date, so it is easy to generate the required name in code. They consist of a list of all the aircraft movements at my local airport during the given day, in time order. Naturally, the same aircraft come and go many times, and the objective is to loop through the files, pick out the first instance of when each individual aircraft appears (the first visit or FV) copy it to a list and then ignore it from then on. The result should be a list of all the first visits in date order. Should be simple, but... My program is small so I am including the entire implementation code.
procedure TForm1.FormCreate(Sender: TObject);
begin
FileDate := StrToDate('01/01/2007');
FName := 'E:LGW Reports/SBSLGW2007-01-01.txt'; //1st file to be read
FDStr := copy(FName, 21, 10);
TempList := TStringList.Create; //temp holder for file contents
FVCheckList := TStringList.Create; //holds unique identifier (UID)
FVCheckList.Sorted := TRUE;
FVCheckList.Duplicates := dupIgnore;
FVList:= TStringList.Create; //the main output
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
Memo1.Lines.Append('Started');
Repeat
TempList.Clear;
TempList.LoadFromFile(FName);
for i := 1 to TempList.Count-1 do
begin
Line := TempList.Strings[i];
//create a //create a Unique identifier (UID) from elements in Line
Serial := Trim(Copy(Line, 22, 9));
MsnPos1 := Pos('[', Line) + 1;
MsnPos2 := Pos(']', Line);
Msn := copy(Line, MsnPos1, (MsnPos2 - MsnPos1));
UID := Serial + '/' + Msn;
//
if (FVCheckList.IndexOf(UID) < 0) then
begin
FVCheckList.Append(UID);
//Add date of file to Line, otherwise it gives no clue when FV was
FVList.Append(FormatDateTime('YYYY-MM-DD', FileDate) + ' ' + Line);
FileDate := IncDay(FileDate, 1);
FName := 'E:LGW Reports/SBSLGW' + FormatDateTime('YYYY-MM-DD', FileDate) + '.txt';
end;
end;
Until FileExists(FName) = FALSE;
FVCheckList.SaveToFile('E:LGW Reports/First Visit Checklist.txt');
FVList.SaveToFile('E:LGW Reports/First Visits.txt');
Memo1.Lines.Append('Finished');
Memo1.Lines.SaveToFile('E:LGW Reports/Files parsed.txt');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TempList.Free;
FVCheckList.Free;
FVList.Free;
end;
There are no compiler errors, it runs to completion in seconds and produces the two text files specified, correctly formatted. The big problem is that the lines actually listed in FVList are not always the very first visit of the aircraft, they can be the first, the most recent or somewhere in between. I cannot see any obvious clue as to why the wrong instance is appearing: if my code is right, then something is wrong with the functioning of TStringList FVCheckList. The fault is far more likely to be something I have overlooked, or my understanding of how .dupIgnore works, or maybe my looping isn't working as it should.
I should be very grateful for any practical help. Many thanks in advance.
Repeat
...
Until FileExists(FName) = FALSE;
Should be
While FileExists(FName) = TRUE do
Begin
End;
If the first 2007-01-01 file does not exist, your code will crash on the first LoadFromFile() since you don't check for the file's existence before loading it, unlike with the subsequent files.
Otherwise, I would suggest sticking with repeat but assign FName at the top of each loop iteration instead of initializing it outside the loop and then reassigning at the bottom of each iteration. No need to duplicate efforts.
If you check IndexOf() manually, you don't need to use Sorted or dupIgnore at all. This is what you should be doing in this situation. When dupIgnore ignores a new string, Append() doesn't tell you that the string was ignored. To do that, you would have to check whether the Count was actually increased or not.
Inside the outer loop, the reassignment of FileDate and FName should be outside of the inner for loop,not inside the for loop at all.
Try this instead:
procedure TForm1.FormCreate(Sender: TObject);
begin
FileDate := EncodeDate(2007,1,1);
FDStr := FormatDateTime('YYYY-MM-DD', FileDate);
TempList := TStringList.Create; //temp holder for file contents
FVCheckList := TStringList.Create; //holds unique identifier (UID)
FVList := TStringList.Create; //the main output
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
Memo1.Lines.Append('Started');
Repeat
FName := 'E:LGW Reports/SBSLGW' + FormatDateTime('YYYY-MM-DD', FileDate) + '.txt';
if not FileExists(FName) then Break;
Memo1.Lines.Append(FName)
TempList.LoadFromFile(FName);
for i := 1 to TempList.Count-1 do
begin
Line := TempList.Strings[i];
//create a Unique identifier (UID) from elements in Line
Serial := Trim(Copy(Line, 22, 9));
MsnPos1 := Pos('[', Line) + 1;
MsnPos2 := PosEx(']', Line, MsnPos1);
Msn := copy(Line, MsnPos1, (MsnPos2 - MsnPos1));
UID := Serial + '/' + Msn;
if FVCheckList.IndexOf(UID) = -1 then
begin
FVCheckList.Append(UID);
//Add date of file to Line, otherwise it gives no clue when FV was
FVList.Append(FormatDateTime('YYYY-MM-DD', FileDate) + ' ' + Line);
end;
end;
FileDate := IncDay(FileDate, 1);
end;
FVCheckList.SaveToFile('E:LGW Reports/First Visit Checklist.txt');
FVList.SaveToFile('E:LGW Reports/First Visits.txt');
Memo1.Lines.Append('Finished');
Memo1.Lines.SaveToFile('E:LGW Reports/Files parsed.txt');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
TempList.Free;
FVCheckList.Free;
FVList.Free;
end;

Save Dbgrid Column Width Values to Ini and Reread them

I have Inherited form and a Ehlib dbgrid on it for selecting-listing records... The form is ready made for a lot of buttons and im using this form with different queries.
Like this...
If Dm.QrTmp.Active then Dm.QrTmp.Active:=False;
Dm.QrTmp.SQL.Clear;
Dm.QrTmp.SQL.Add(' SELECT ');
Dm.QrTmp.SQL.Add(' ch.cari_RECno AS KayitNo ');
Dm.QrTmp.SQL.Add(' FROM CARI_HESAPLAR ch ');
if FrmTmp=nil then FrmTmp:=TFrmTmp.Create(Self);
FrmTmp.StatusBar.Hide;
Dm.QrTmp.Open;
FrmTmp.DbGrid.DataSource:=Dm.DsQrTmp;
This query is cutted down but i have of course use a lot of fields. And Queries changes alot of time in the application.
The problem is column width. Manager wants to set column widths and restore them again. Actually my grid component supports save - restore column properties but as you can see my usage i m not using static columns. also i dont want to use xgrid.columns[0].width percent by percent.
Im using a ini in may app.
I want to add new section on it and named "Gridwidth"...
[Gridname]
Colwidths=x,y,z (where they are width values)
I'm now coding this line by line.
My write procedure is like this.
With dbgridx do
begin
For i:=0 to columns.count-1
begin
widthstr:=widthstr+Column[i].width+',';
end;
end;
Widthstr will be "15,23,45,67" etc...
But i want to know if this is good solution and if somebody know a better way and has some good code.
This should do it:
uses
IniFiles;
const
SETTINGS_FILE = 'Edijus\Settings.ini';
procedure TForm1.LoadDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
_MemIniU: TMemIniFile;
_SettingsPath: string;
i, j: integer;
_ParentClass: TWinControl;
begin
_SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
if (not Assigned(ADBGrid)) or (not Assigned(ADBGrid.DataSource)) or
(not Assigned(ADBGrid.DataSource.DataSet)) then
Exit;
_MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
try
_ParentClass := ADBGrid.Parent;
while not(_ParentClass is TForm) do
_ParentClass := _ParentClass.Parent;
for i := 0 to Pred(ADBGrid.DataSource.DataSet.Fields.Count) do
for j := 0 to Pred(ADBGrid.Columns.Count) do
begin
if (ADBGrid.DataSource.DataSet.Fields[i].FieldName = ADBGrid.Columns[j]
.FieldName) then
ADBGrid.Columns[j].Width :=
_MemIniU.ReadInteger(_ParentClass.Name + '_' + ADBGrid.Name,
ADBGrid.Columns[j].FieldName, 64);
end;
finally
FreeAndNil(_MemIniU);
end;
end;
procedure TForm1.SaveDBGridColumnsWidth(const ADBGrid: TDBGrid);
var
_MemIniU: TMemIniFile;
_SettingsPath: string;
i: integer;
_ParentClass: TWinControl;
begin
_SettingsPath := GetHomePath + PathDelim + SETTINGS_FILE;
if (not Assigned(ADBGrid)) or
(not ForceDirectories(ExtractFilePath(_SettingsPath))) then
Exit;
_MemIniU := TMemIniFile.Create(_SettingsPath, TEncoding.UTF8);
try
_ParentClass := ADBGrid.Parent;
while not(_ParentClass is TForm) do
_ParentClass := _ParentClass.Parent;
for i := 0 to Pred(ADBGrid.Columns.Count) do
if (ADBGrid.Columns[i].FieldName <> '') then
_MemIniU.WriteInteger(_ParentClass.Name + '_' + ADBGrid.Name,
ADBGrid.Columns[i].FieldName, ADBGrid.Columns[i].Width);
_MemIniU.UpdateFile;
finally
FreeAndNil(_MemIniU);
end;
end;

Why doesn't my TStringList gets sorted

I Have a TStringList I create on FormCreate
ScriptList := TStringList.Create;
In another function in my program after I have loaded strings into the list I have the following code
ScriptList.Sorted := True;
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
ShowMessage(ScriptList[i]);
But the list is not sorted
Why is that?
Edited:
Filling the list is done by the following code
function TfrmMain.ScriptsLocate(const aComputer: boolean = False): integer;
var
ScriptPath: string;
TempList: TStringList;
begin
TempList := TStringList.Create;
try
if aComputer = True then
begin
ScriptPath := Folders.DirScripts;
Files.Search(TempList, ScriptPath, '*.logon', False);
ScriptList.AddStrings(TempList);
end
else
begin
if ServerCheck then
begin
ScriptPath := ServerPath + 'scripts_' + Network.ComputerName + '\';
Folders.Validate(ScriptPath);
TempList.Clear;
Files.Search(TempList, ScriptPath, '*.logon', False);
ScriptList.AddStrings(TempList);
Application.ProcessMessages;
ScriptPath := ServerPath + 'scripts_' + 'SHARED\';
Folders.Validate(ScriptPath);
TempList.Clear;
Files.Search(TempList, ScriptPath, '*.logon', False);
ScriptList.AddStrings(TempList);
end;
end;
finally
TempList.Free;
end;
ScriptList.Sort;
Result := ScriptList.Count;
end;
The filesearch function:
function TFiles.Search(aList: TstringList; aPathname: string; const aFile: string = '*.*'; const aSubdirs: boolean = True): integer;
var
Rec: TSearchRec;
begin
Folders.Validate(aPathName, False);
if FindFirst(aPathname + aFile, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
aList.Add(aPathname + Rec.Name);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
Result := aList.Count;
if not aSubdirs then Exit;
if FindFirst(aPathname + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then
Files.Search(aList, aPathname + Rec.Name, aFile, True);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
Result := aList.Count;
end;
The main problem is that the list is filled OK with the items I want, but it never gets sorted.
When you set Sorted to True you are saying that you want the list to be maintained in order. When new items are added, they will be inserted in order. When Sorted is True, the Sort method does nothing because the code is built on the assumption that the list is already order.
So, in your code calling Sort does nothing and could be removed. However, I would take the alternative approach, remove the setting of Sorted and call Sort explicitly:
ScriptList.LoadFromFile(...);
ScriptList.Sort;
for i := 0 to ScriptList.Count - 1 do
...
Now, in fact I think that your code is not quite as you have claimed. You claim that you load the file, and then set Sorted to True. That cannot be the case. Here is the SetSorted implementation:
procedure TStringList.SetSorted(Value: Boolean);
begin
if FSorted <> Value then
begin
if Value then Sort;
FSorted := Value;
end;
end;
So, if Sorted is False when you set it to True, the list will be sorted.
But even that does not explain what you report. Because if Sorted is True when you call LoadFromFile, each new line will be inserted in order. So, what you report in the question cannot be the whole story.
Unless you are making subsequent additions to the list, it is cleaner, in my view, to ignore the Sorted property. Leave Sorted as its default value of False. And call Sort when you want to enforce an ordering to the list. All the same, it might be worth digging a bit deeper to understand why your assertions in the question don't tally with the implementation of TStringList.

delphi 7: How can I find item of object collection?

how can I find by name and get the Item in a collection of object ?
procedure TfoMain.InitForm;
begin
// Liste des produits de la pharmacie 1
FListeDispoProduit := TListeDispoProduit.Create(TProduit);
with (FListeDispoProduit) do
begin
with TProduit(Add) do
begin
Name := 'Produit 01';
CIP := 'A001';
StockQty := 3;
AutoRestock := 1;
QtyMin:= 2;
end;
with TProduit(Add) do
begin
Name := 'Produit 02';
CIP := 'A002';
StockQty := 5;
AutoRestock := 0;
QtyMin:= 2;
end;
function getProductByName(productName: String): TProduit;
var
i : integer;
begin
for i := 0 to fProductList.Count -1 do
begin
if (TProduit(fProductList.Items[i]).Name = productName)
Result :=
end;
end;
I want to edit qty about a product name.
How can I do this?
thank you
If your collection object is a TCollection, then it has an Items property (which you should have been about to see in the documentation, or in the source code). Use that and its Count property to write a loop where you inspect each item to see whether it matches your target.
var
i: Integer;
begin
for i := 0 to Pred(FListeDespoProduit.Count) do begin
if TProduit(FListeDespoProduit.Items[i]).Name = productName then begin
Result := TProduit(FListeDespoProduit.Items[i]);
exit;
end;
end;
raise EItemNotFound.Create;
end;
Items is a default property, which means you can omit it from your code and just use the array index by itself. Instead of FListeDespoProduit.Items[i], you can shorten it to just FListeDespoProduit[i].
function getProductByName(productName: String): TProduit;
var
i : integer;
begin
for i := 0 to fProductList.Count -1 do
begin
if (TProduit(fProductList.Items[i]).Name = productName)
Result := TProduit(fProductList.Items[i]); // this???
end;
end;
You can then go:
MyProduit := getProductByName('banana');
MyProduit.StockQty := 3;
Or whatever you wish.
Your TProduit implements (Add). It doesn't already implement (Get) (or something similar)?
Are you inheriting this code? Is there more detail?
Edit: otherwise you'll have to create the Get procedure yourself, possibly by looping over the list and finding a match, then returning it.

Resources