How to check value and priority and assign correct value

Hi everyone,

Hope someone can help.

I have been racking my head for the past couple of days but can’t seem to get my head around how to make this work/code this as easy as possible.

I have a list as follows:
Value (Priority)
0 (Priority 8)
1 (Priority 6)
2 (Priority 5)
3 (Priority 7)
4 (Priority 1)
5 (Priority 2)
6 (Priority 3)
5 (Priority 2)
6 (Priority 3)
7 (Priority 4)
8 (Priority 3)
9 (Priority 3)

Priority 8 is the lowest priority and 1 is the highest priority.

How it is supposed to work:

  1. If the current value is 1 (Priority 6) and the new value is 5 (Priority 2) then the current value must be replaced with 5 (Priority 2).
  2. If the current value is 3 (Priority 7) and the new value is 0 (Priority 8) then the current value must stay as 3 (Priority 7).

Any help will be appreciated.

Regards

Johan de Klerk

How is the value selected, what triggers a change?

Drop Down lists boxes and Radio controls make it easy for the user to change, but I’m also left wondering if you have some code which runs which changes the value when certain criteria is met or event occurred. Is this the case?

Hi,

I import a file a file with values from 0 to 9 and check these values against current values in my table.
When the value from the import file is different then It needs to check according to the priority if the value in my table needs to be replaced or not.

So is the import file the same order as what you have in a file?

What file type is your import file?
What file type is your existing data file?

Maybe it would help if you could explain the challenge that you’re facing with this problem?

If I understood correctly, you could save the current value and current priority and compare against that, something like:

  currentPriority = 99 
  LOOP ...
    ...
    IF listPriority < currentPriority
      currentValue = listValue
      currentPriority = listPriority
    END
  END

HTH

Import file is a CSV.
Existing data is TPS.

I have tried with lots and lots of IF statements but it got to complicated.
I just can’t get my head around how to make it work in a simple way.

The values and priorities are not in any table.
It is an indication of what the value must be according to the priority.
I could probably create a Queue with the values and priority then your code would work.
But I would prefer to not create another Queue.

Is it more complicated than something like:

SetRowValue PROCEDURE(LONG pRowID, LONG pValue, LONG pPriority)

   CODE

 ! Fetch the row with the pRowID

  IF pPriority > Row.Priority
     Row.Value = pValue
     Row.Priority = pPriority
  END

! Write the value

So you can identify the TPS record easily?

If so, why not just overwrite the value? No need for IF statements then.

I over complicate things sometimes, its the nature of the work I think.

  ! Lower Priority numbers are best 
 IF GetPriorty( CurrentValue ) > GetPriorty( NewValue ) THEN
    CurrentValue = NewValue
 END

GetPriority PROCEDURE(BYTE Value) !,BYTE
Priority BYTE
  Code
  CASE Value
  OF 0 ; Priority = 8
  OF 1 ; Priority = 6
  OF 2 ; Priority = 5
  OF 3 ; Priority = 7
  OF 4 ; Priority = 1 ! Highest / Best
  OF 5 ; Priority = 2
  OF 6 ; Priority = 3
  OF 7 ; Priority = 4
  OF 8 ; Priority = 3
  OF 9 ; Priority = 3
  END
  RETURN Priority

Hi Rich/Jeff/Carlos/Carl,

Thanks to everyone for all your ideas and advice.

I have implemented Carl’s solution above and it works perfect.
Thanks Carl, you have nailed what I needed.

Regards

Johan de Klerk

the only problem I can see with this code is that if an invalid new value exists, it will have a priority of 0 and therefore the CurrentValue will be overwritten.

this can be fixed by having an ELSE which sets priority to 9 (or another high value).

eg. something like:

GetPriority PROCEDURE(BYTE Value) !,BYTE
Priority BYTE, AUTO
  Code
  CASE Value
  OF 0 ; Priority = 8
  OF 1 ; Priority = 6
  OF 2 ; Priority = 5
  OF 3 ; Priority = 7
  OF 4 ; Priority = 1 ! Highest / Best
  OF 5 ; Priority = 2
  OF 6 ; Priority = 3
  OF 7 ; Priority = 4
  OF 8 ; Priority = 3
  OF 9 ; Priority = 3
  ELSE  ! invalid
         Priority = 9
  END
  RETURN Priority

another thought is to use EXECUTE

GetPriority PROCEDURE(BYTE Value) !,BYTE
Priority BYTE, AUTO
  Code
  EXECUTE Value+1
    Priority = 8
    Priority = 6
    Priority = 5
    Priority = 7
    Priority = 1 ! Highest / Best
    Priority = 2
    Priority = 3
    Priority = 4
    Priority = 3
    Priority = 3
  ELSE  ! invalid
    Priority = 9
  END
  RETURN Priority

you could probably do something similar using choose:

priority =  choose(value+1,8,6,5,7,1,2,3,4,3,3,9)

note the extra value ‘9’ on the end to catch invalid input.

yet another way might be to use an array

priorities string('<8,6,5,7,1,2,3,4,3,3>')
priority   byte,dim(10),over(priorities)

Clarion arrays are 1-based (unlike C and derivatives) so you add one

  if NewValue < 10 and priority[CurrentValue+1] > priority[NewValue+1]
    CurrentValue = NewValue
  end

if you are not sure the current value is valid then you might want to validate that too:

  if CurrentValue > 9  ! invalid current value
    if NewValue < 10
      CurrentValue = NewValue
    end
  elsif NewValue < 10 and priority[CurrentValue+1] > priority[NewValue+1]
    CurrentValue = NewValue
  end

Doing it this way saves the overhead of a function call as you don’t need the GetPriority procedure. Admittedly that overhead is small…

anyway there’s some more ideas.

Hi Geoff,

Thank you very much for your reply and more ways to get the results.
I am already checking for invalid values in the calling section so it should stop before even getting to the check part.
But I am definitely going to add the error checking part in the called procedure as well, thanks.

Regards

Johan de Klerk