PDA

View Full Version : number match selection



longpole001
- 12th April 2014, 23:30
need to do some code which is giving me an issue

i want to find a value of 1-6 in a variable in an array and if the value has been used , then it is removed from the list as an available value

the lowest value that has not been used is then returned as the answer

regards

Sheldon

though something like this as a starting point

for z = 0 the index
if value1(z) & $F0 = value2 then
y = value1(z) & $0F
if Y = 1 then not_free = 1
if y = 2 then not_free = 2
if y = 3 then not_free = 3
if y = 4 then not_free = 4
if y = 5 then not_free = 5
if y = 6 then not_free = 6
endif
next z

Amoque
- 13th April 2014, 11:01
On way is to use bit flags to keep track of what numbers have been guessed:

GUESSES = %01111110 ' We're not using bit 0 because you can't guess 0

On each guess, GUESSES.[Numberguessed] = 0. If 3 is guessed GUESSES.3 = 0 and GUESSES becomes %01110110...

To determine the lowest number and return it

IF GUESSES = 0 then all numbers have been guessed
For LP = 1 to 6
IF GUESSES.LP = 1 Then
LP is the lowest number left
Endif
Next LP

I'm not advertising this as suitable code, rather sort of pseudo-code to illustrate the concept.

longpole001
- 14th April 2014, 01:39
thanks for that - sometime i get caught up in what i am doing and cant think of easy answer

here is simulated code ill try,my real codes has multi indexed variables ,but the principle is correct
the end value shown needs to be the lowest number between 1 -6 for my use , 0 is ok as it show there is no " index slots" Left and is used to determine if more index slots are added later



z = 0
X = 0
index_slot = 0

for z = 0 the index ' test to array length
if value1(z) & $F0 = value2 then ' match upper nibble
for X = 1 to 6
Y = value1(z) & $0F ' get lower nibble value into Y
if X = Y then Index_slot.X = 1 ' put a 1 into the bit location representing values 1- 6 of index_slot that have been used
next X
next z

x = 0
for X = 6 to 1
if index_slot.X >> X = 1 then index_slot = X ' show the lowest value available index slot for use
next X

longpole001
- 15th April 2014, 07:30
i get a " bad token" error with this , what is the correct way to do it again ?




for X = 6 to 1
if index_slot.X >> X = 1 then index_slot = X ' show the lowest value available index slot for use
next X

longpole001
- 15th April 2014, 08:09
for X = 6 to 1
if index_slot.0[X] >> X = 1 then index_slot = X ' show the lowest value available index slot for use
next X

Amoque
- 17th April 2014, 05:55
Don't forget to add a" STEP -1" to your "FOR" line, unnecessary when counting up, but required to decrement your loop variable