I have the following code that takes an array, totals all the elements, and writes to a text file. Works great! However, it's not exactly what I need!
What I need to do is break the elements into groups of 50 (plus one "remainder" group of less than 50) and change certain text file pieces for each of the groups. So, take the entire array, divide by 50 to get the number of groups, for each group get the total, write the header record, write the detail of the elements, switch to the next group, total it, write the header record, write the detail records, etc.
Can someone please help me get my head around the loop logic needed to do these calculations? I've been working on it for days and just can't get it quite right!
Here is the existing code that creates a single group:
Leslie
landrews@metrocourt.state.nm.us
There are 10 types of people in the world -
those who understand binary
and
those who don't!
What I need to do is break the elements into groups of 50 (plus one "remainder" group of less than 50) and change certain text file pieces for each of the groups. So, take the entire array, divide by 50 to get the number of groups, for each group get the total, write the header record, write the detail of the elements, switch to the next group, total it, write the header record, write the detail records, etc.
Can someone please help me get my head around the loop logic needed to do these calculations? I've been working on it for days and just can't get it quite right!
Here is the existing code that creates a single group:
Code:
procedure CreateTextFile(AList : variant; AVoucher : string);
var
commonstring, headerstring, detailstring : string;
VoucherList : TStringList;
i : integer;
vouchertotal, milespay, jurorpay : double;
VoucherNumber, intGroups : integer;
formattotal : string;
const headerstringformat = '%-2s%-3s%-11s%-2s%-2s%-2s%-2s%-2s%-2s%-4s%14s%-14s%-4s%-3s%-4s%-2s%-9s%-4s%-4s%-2s%-4s%-2s%-8s%-4s%-4s%-27s';
const detailstringformat = '%-10s%-1s%-12s%-11s%-2s%-1s%-1s%-2s%-1s%-2s%-30s%-29s%-30s%-18s%-2s%-10s%-14s%-3s';
const JurorsPerGroup = 50;
begin
VoucherList := TStringList.Create;
vouchertotal := 0;
VoucherNumber := strToINt(copy(Avoucher,8,6));
Inc(VOucherNumber);
AVoucher := copy(AVoucher,1,2) + copy(AVoucher,4,3) + IntToStr(VoucherNumber);
headerstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['D', 'MP', '218', 'MP', '218', AVoucher]);
commonstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['L', 'MP', '218', 'MP', '218', AVoucher]);
For i := 0 to personcount - 1 do
begin
vouchertotal := vouchertotal + AList[i, 4] + AList[i, 6];
end;
vouchertotal := vouchertotal * 100;
formattotal := floatToStr(voucherTotal);
while length(formattotal) < 14 do formattotal := '0' + formattotal;
headerstring := headerstring +
Format(headerstringformat,
['MP','218', AVoucher, FormatDateTime('mm', Today), FormatDateTime('dd', Today), FormatDateTime('yy', Today), '', '', copy(AVoucher,1,2), '', formattotal, '', '012','218','P559','','400','','4791','','','','','','','Description']);
VoucherList.Add(headerstring);
For i := 0 to personcount - 1 do
begin
jurorpay := AList[i, 4];
milespay := AList[i, 6];
vouchertotal := jurorpay + milespay;
if vouchertotal = 0 then Continue
else begin
vouchertotal := vouchertotal * 100;
formattotal := floatToStr(vouchertotal);
while length(formattotal) < 14 do formattotal := '0' + formattotal;
detailstring := commonstring + Format(detailstringformat,
['MP218', '', '', 'MP' + copy(AVoucher,1,2) + '#', '', '','','','','', UpperCase(AList[i,11]), UpperCase(AList[i, 1]), AList[i,8], '', AList[i,9], AList[i,10], formattotal, '']);
VoucherList.Add(detailstring);
end;
end;
VoucherList.SaveToFile('C:\vouchertest.txt');
VoucherList.Free;
end;
Leslie
landrews@metrocourt.state.nm.us
There are 10 types of people in the world -
those who understand binary
and
those who don't!