![]() |
|
#1
|
|||
|
|||
|
i need to create totals from records read in from a text file.
code field, description field, amount field, date field 0001 WIDGETS A 1500.00 9/1/09 0002 WIDGETS B 200.00 9/1/09 0003 WIDGETS C 350.00 9/1/09 0001 WIDGETS A 1250.00 9/2/09 0002 WIDGETS B 375.00 9/2/09 0003 WIDGETS C 400.00 9/2/09 I am needing to keep totals by the code field and by the date field. Currently i am using stringlists and doing a search thru them to match up the code or date and then adding the amount field to the next element in the stringlist. stringlist1 stringlist2 (after reading 3 records) 0001 9/1/09 1500 2050 0002 200 0003 350 is there a better way? |
|
#2
|
|||
|
|||
|
Just add the data to a (Client)DataSet... Then you can add complete records...
If you don't know how to, you can also add records to a TStringList, but then I think I would switch to a TObjectList very quickly... But you initial method is good, you must have the data in memory, not in the file... |
|
#3
|
|||
|
|||
|
however i was really hoping for some kind of super unknown function/procedure that did all the grunt work. the text file actually has 9 different fields in it that the client wants totaled and is subject increase depending on how much more info they can cram into it.
Anyways, thanks again... |
|
#4
|
|||
|
|||
|
Well, if it's space delimited and in 1 line is only 1 space you can always try the TStringList.DelimitedText with TStringList.Delimiter = ' '. Then you'll have the structure already in a TStringList, Strings[0]=0001, Strings[1]=WIDGETS Strings[2]=A, and so on...
Otherwise it's old-fashioned string slicing... |
|
#5
|
|||
|
|||
|
since i am having to keep up with the element in the array for adding up numbers and so forth. i have been trying to figure out how to use the tobjectlist to perform that which i need.
i have messed with records and am wondering if i could use them in the objectlist or should i create and array of my records.... code_totals = record code = string; total1 = single; total2 = integer; end; how could i go about implimenting the record (adding new records, updating records, processing all records start to end...) |
|
#6
|
|||
|
|||
|
Using a TObjectList (sample not tested, typed here directly):
Code:
type
TCodeTotals = class
private
FCode: String;
FTotal1: Integer;
FTotal2: Integer;
public
constructor Create(ACode: String; Total1: Single; Total2: Integer;
published
property Code: String read FCode write FCode;
property Total1: Single read FTotal1 write FTotal1;
property Total2: Integer read FTotal2 write FTotal2;
end;
TTotals = class(TObjectList);
private
function GetItems(AIndex: Integer): TCodeTotals;
procedure SetItems(AIndex: Integer; AValue: TCodeTotals);
public
function IndexOfCode(ACode: String): Integer;
property Items[Index: Integer] read GetItems write SetItems; default;
end;
constructor TCodeTotals.Create(ACode: String; ATotal1: Single; ATotal2: Integer);
begin
inherited;
FCode := ACode;
FTotal1 := ATotal1;
FTotal2 := ATotal2;
end;
function TTotals.GetItems(AIndex: Integer): TCodeTotals
begin
Result := TCodeTotals(inherited(Items[AIndex]));
end;
procedure SetItems(AIndex: Integer; Value: TCodeTotals);
begin
inherited Items[AIndex] := Value;
end;
function IndexOfCode(ACode: String): Integer;
var
i: Integer;
begin
for i := ItemCount - 1 downto 0 do
begin
if SameText(Items[i].Code, ACode) then
begin
Result := i;
Exit;
end;
end;
Result := -1;
end;
Code:
MyTotals.Add(TCodeTotals.Create('a code', 0.0, 1));
Code:
MyTotals[0].Code := 'a new code'; Code:
for i := 0 to MyTotals.ItemCount - 1 do begin ShowMessage(MyTotals[i].Code); end; |
![]() |
| Thread Tools | |
| Display Modes | |
|
|