Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with nested loop logic

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
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:

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!
 
Well, I finally got it working!!! Did the happy programmer dance and everything! Here's the code I ended up with (it's not pretty, but it gets the job done!)

By using

Code:
AList[i + (JurorsPerGroup * j)

I can get the array element that I need. For instance if I'm on the first person of the first group then i = 0 and j = 0 so the expression evaluates to 0, the first element of the array. If I'm on the fifth person of the second group then i = 5, j = 1 so the expression evaluates to element 55 of the original array! The last group (the hardest since it may have from 1 - 49 people) I had to count the elements (done in a previous function = finalcounter) which is the high end of my last loop, and to get the starting point I multiple the number of groups and the number of people on each group. So if I have 2 groups in the main loop times the number of people on each group, I would start at 100 and go to the finalcounter value.

Hopefully this can help someone else out some day!

leslie

Code:
procedure CreateTextFile(AList : variant; AVoucher : string);
var
commonstring, headerstring, detailstring, VoucherString : string;
VoucherList : TStringList;
i, j : integer;
vouchertotal, detailtotal, 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;
  VoucherString := copy(AVoucher,1,2) + copy(AVoucher,4,3);
  VoucherNumber := strToINt(copy(Avoucher,8,6));
  intGroups := finalcounter div JurorsPerGroup;
  For j := 0 to intGroups - 1 do
  begin
    vouchertotal := 0;
    Inc(VOucherNumber);
    AVoucher :=  VoucherString + IntToStr(VoucherNumber);
    For i := 0 to JurorsPerGroup - 1 do
    begin
      vouchertotal := vouchertotal + AList[i + (JurorsPerGroup * j), 4] + AList[i + (JurorsPerGroup * j), 6];
    end;

      vouchertotal := vouchertotal * 100;
     formattotal := floatToStr(voucherTotal);
     while length(formattotal) < 14 do formattotal := '0' + formattotal;
     headerstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['D', 'MP', '218', 'MP', '218', AVoucher]);
     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 JurorsPerGroup - 1 do
    begin
      jurorpay := AList[i + (JurorsPerGroup*j), 4];
      milespay := AList[i + (JurorsPerGroup*j), 6];
      detailtotal := jurorpay + milespay;
      if detailtotal = 0 then Continue
      else begin
        detailtotal := detailtotal * 100;
        formattotal := floatToStr(detailtotal);
        while length(formattotal) < 14 do formattotal := '0' + formattotal;
        commonstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['L', 'MP', '218', 'MP', '218', AVoucher]);
        detailstring := commonstring + Format(detailstringformat,
        ['MP218', '', '', 'MP' + copy(AVoucher,1,2) + '#', '', '','','','','', UpperCase(AList[i + (JurorsPerGroup*j),11]), UpperCase(AList[i + (JurorsPerGroup*j), 1]), AList[i + (JurorsPerGroup*j),8], '', AList[i + (JurorsPerGroup*j),9], AList[i + (JurorsPerGroup*j),10], formattotal, '']);
        VoucherList.Add(detailstring);
      end;
    end;
  end;
  vouchertotal := 0;
  for i := (JurorsPerGroup * intGroups) to finalcounter - 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 := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['D', 'MP', '218', 'MP', '218', AVoucher]);
  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 := (JurorsPerGroup * intGroups) to finalcounter - 1 do
  begin
    jurorpay := AList[i, 4];
    milespay := AList[i, 6];
    detailtotal := jurorpay + milespay;
    if detailtotal = 0 then Continue
    else begin
      detailtotal := detailtotal * 100;
      formattotal := floatToStr(detailtotal);
      while length(formattotal) < 14 do formattotal := '0' + formattotal;
      commonstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['L', 'MP', '218', 'MP', '218', AVoucher]);
      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;
 
You might want to think about breaking this routine apart.

You could have the calculation logic in the main body calling other procedures that did the formatting, etc.

Cheers

Cheers
 
I'm sure in the future I will, but for now I'm just glad I got it to work!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top