By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

logic design flow help needed

P: n/a
I'm having a brain freeze on how to go about creating the logic for
evenly distributing a group of items in rotation through multiple
iterations. I'm particularly looking for logic flow help and not
necessarily syntax examples.

Let's say we have a database table with the following pieces of fruit:

apple
apple
banana
orange
pear
peach

First off, I need to move any duplicate pieces of fruit to the end of
the list:

apple
banana
orange
pear
peach
apple

Note the second apple goes to the back of the line.

Now let's say there's 5 baskets to distribute the fruit in. Each basket
can only contain one unique piece of fruit at a time. So on the first
iteration the baskets look like this:

basket1: apple
basket2: banana
basket3: orange
basket4: pear
basket5: peach
apple (no basket)

I now need to shift every piece of unique fruit up by one, bringing the
5th piece to the front of the line. Note that second apple stills stays
at the back of the line because it's a duplicate.

peach
apple
banana
orange
pear
apple

The next basket distribution iteration looks like this:

basket1: peach
basket2: apple
basket3: banana
basket4: orange
basket5: pear
apple (no basket)

....and so on. The second apple would only be added to the rotation if 1)
one of the pieces of fruit gets eaten (deleted) OR 2) a sixth basket is
added to the rotation.

Here's the flow.

1) query table for data, ORDER BY fruit (alphabetically)
2) loop through recordset array and move any duplicates to the back
(exp. compare 1 with 2, if 2 equals 1, move 2 to the end)
3) distribute fruit in baskets
4) move fruit 1 to the back, but ahead of the duplicates
5) go to step 3

How much of the sorting (and resorting) requirements can be done in the
query? Would it be easier to re-query on each iteration or manipulate
the existing recordset array?

Any input would be greatly appreciated.

Thanks!
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Xenophobe wrote:
1) query table for data, ORDER BY fruit (alphabetically)
2) loop through recordset array and move any duplicates to the back
(exp. compare 1 with 2, if 2 equals 1, move 2 to the end)
3) distribute fruit in baskets
4) move fruit 1 to the back, but ahead of the duplicates
5) go to step 3

How much of the sorting (and resorting) requirements can be done in the
query? Would it be easier to re-query on each iteration or manipulate
the existing recordset array?

Any input would be greatly appreciated.


I suggest you try comp.programming where you certainly will get better
and more detailed methods.
I'd probably try some thing like this:

1) query table ordered alphabetically with an extra 'zero' column
2) iterate the data updating extra column with number of previous
repetitions, so you get

apple 0
apple 1
apple 2
banana 0
orange 0
orange 1
pear 0
peach 0

2a) reorder by repetitions then name, so you get

apple 0
banana 0
orange 0
pear 0
peach 0
apple 1
orange 1
apple 2

3) 4) and 5) same as yours 3) 4) and 5)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
On 2004-02-26, Xenophobe wrote:

Now let's say there's 5 baskets to distribute the fruit in. Each basket
can only contain one unique piece of fruit at a time. So on the first
iteration the baskets look like this:

basket1: apple
basket2: banana
basket3: orange
basket4: pear
basket5: peach
apple (no basket)

Would it not be possible to use two arrays fruitbasket[] and
fruitnobasket[]?
I now need to shift every piece of unique fruit up by one, bringing the
5th piece to the front of the line. Note that second apple stills stays
at the back of the line because it's a duplicate.

peach
apple
banana
orange
pear
apple
It would seem to make this part easier.
The next basket distribution iteration looks like this:

basket1: peach
basket2: apple
basket3: banana
basket4: orange
basket5: pear
apple (no basket)

or what about an associative array:
fruit[b1] = peach
fruit[b2] = apple
..
..
fruit[b5] = pear
fruit[nb1] = apple
...and so on. The second apple would only be added to the rotation if 1)
one of the pieces of fruit gets eaten (deleted) OR 2) a sixth basket is
added to the rotation.

Here's the flow.

1) query table for data, ORDER BY fruit (alphabetically)
2) loop through recordset array and move any duplicates to the back
(exp. compare 1 with 2, if 2 equals 1, move 2 to the end)
3) distribute fruit in baskets
4) move fruit 1 to the back, but ahead of the duplicates
5) go to step 3

How much of the sorting (and resorting) requirements can be done in the
query? Would it be easier to re-query on each iteration or manipulate
the existing recordset array?

As far as the information you've given goes only one query is needed
unless the data in your table is likely to change at any point in the
process. It would seem easier to manipulate the array or arrays.

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.