× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Barbara,

Thanks first of all for taking the time to give a detailed reply to my proposed solution.

The "solution would work for the exact data " because that is all I had to work with and it was an answer to his question. It is now up to Dave to implement the solution (if he so desires) adjusting it to suit his particular circumstances. In answering questions on these forums, my approach is to try not to read too much background from the question, rather answer the question closely and let others adapt to suit.

While your solution is more robust, Barbara, I would point out that %subarr() was introduced in V5R3 - not all shops have migrated, even at this stage.

Regards,
John McKay
www.mckaysoftware.ie
www.rpglanguage.com

----- Original Message ----- From: "Barbara Morris" <bmorris@xxxxxxxxxx>
To: <rpg400-l@xxxxxxxxxxxx>
Sent: Friday, November 14, 2008 5:51 PM
Subject: Re: SORTA


John McKay wrote:
As far as I can see, the code in the answer does not add the values
together.
...
C specs
sorta acc;
for x = 1 to 3;
y = *zeros;
z = x + 1;
y = %lookup(acc(x): acc: z);
if y > *zeros;
sum$(x) += sum$(y);
sum$(y) = *zeros;
acc(y) = *blanks;
endif;
endfor;


John, I think your solution would work for the exact data that David
showed, but I don't think it would work if there were more than two
values the same. Say the acc values were A B B B, then it would add
sum(3) to sum(2), but it should also add sum(4) to sum(2).

But also, I don't think it would work more than once, since it would
have blank "unused" acc values that would get sorted to the beginning of
the array.

Here's my attempt at a solution, completely untested ...

Since the number of "real" elements in the array is not fixed (starts
with 4 elements, ends up with 3 elements), we need a variable to keep
track, say num_elems.

// sort the array by acc. If there is more than one element
// for any one acc, they will now be together in the array
sorta %subarr(acc : 1 : num_elems);

// merge the sum values for elements with the same acc value
i = 1;
remove_elems = 0;
dow i < num_elems;
// loop through the subsequent elements with the same
// acc value, and add all the sums to sum(i)
for j = (i + 1) to num_elems;
if acc(j) = acc(i);
sum(i) += sum(j);
// now that sum(j) is added to sum(i)
// we want to get rid of element j
// (but just mark it to be removed later)
acc(j) = *hival;
remove_elems += 1;
else;
// we have a new acc
leave;
endif;
// continue with the element after the
// last one we merged
i = j;
endfor;
enddo;

// re-sort the array to put the discarded ones
// at the end (they were set to *hival),
// then adjust the number of actual elements
sorta %subarr(acc : 1 : num_elems);
num_elems -= remove_elems;

--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.