Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-14-2009, 04:22 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 137
Default creating totals from text file...

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?
Reply With Quote
  #2  
Old 09-15-2009, 02:04 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

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...
Reply With Quote
  #3  
Old 09-15-2009, 02:13 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 137
Default Thanks Norrit

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...
Reply With Quote
  #4  
Old 09-15-2009, 02:16 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

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...
Reply With Quote
  #5  
Old 09-15-2009, 08:06 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 137
Default i am interested in the tobjectlist.

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...)
Reply With Quote
  #6  
Old 09-16-2009, 07:15 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

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;
And to add a record to the TObjectList just do:
Code:
MyTotals.Add(TCodeTotals.Create('a code', 0.0, 1));
Updating is nothing more then:
Code:
MyTotals[0].Code := 'a new code';
And processing all records start to end is a simple loop:
Code:
for i := 0 to MyTotals.ItemCount - 1 do
begin
  ShowMessage(MyTotals[i].Code);
end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:39 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.