Largest 5 values from set of Values

Hi, 

Currently i am working on college project, where i am getting 30  field values as input signals for DeltaV.

I want to compare those 30 values and get 5 largest values from those 30 values.

Is there any standard block which can help me to get the above mentioned results.

I know Calc block can help but i am not good with the coding.

Please kindly let me know in case if someone can help.

Thank you for your help and time. 

  • You have to use Calc :) or try this in SFC.

  • In reply to Roman Kostuniak:

    You will need iterative logic provided best in a calc block or other structured text.

    1. Save all 30 values to the first column of an array type parameter (using an action/calc block) with 30 rows and two columns.

    2. In a calc block iterate using a 'for loop' through each value (i). Set a temporary variable "rank" in the calc = 30

    3. In a nested 'for loop' iterate through every other value (j)

    4. compare i to j using max.  When i > j, subtract 1 from "rank".  If i is greater than all j's then rank = 1.  when it is less than all j's it will = 30.

    5. Store the value of "rank" to the second column  of the ith element of the array at the end of the outer 'for loop'

    5. Have another calc block iterate through the second column of array.  If the rank = 1,2,3,4,5 then output the first column to the respective calc output.

  • In reply to Youssef.El-Bahtimy:

    Thanks for you reply. I will try doing the same and post the results ASAP.

    In case of any queries i will post again.

  • In reply to Ashish P:

    Hello  Youssef.El-Bahtimy,

    I tried to implement what you said but was not able to do.

    Cannot use FOR loop in DeltaV calc block, so tried the same using  WHILE ... DO loop. Still not able to get the output.

    Can you please help.

  • In reply to Ashish P:

    Post your code.  I will take  a look.

  • In reply to Youssef.El-Bahtimy:

    Hello  Youssef.El-Bahtimy,

    Please check below mentioned code :  ( only the LOGIC part is specified here)

    IF IN1 = 1 THEN

       I := '^/I.CV' + 1 ;

       J := '^/J.CV' + 1 ;

       RANK := 16;

    WHILE I <= 16 DO

      WHILE J <= 16 DO

      IF '^/FEILD_VALUE'Idea[1] > '^/FEILD_VALUE'[J][1] THEN

    RANK := RANK-1

            END_IF;

                      J := J+1;

      END_WHILE;

            '^/FEILD_VALUE'Idea[2] := RANK;

                      I := I+1;

    END_WHILE ;

    ENDIF;

  • In reply to Ashish P:

    Very Close... Here is my suggestion:

    IF IN1 = 1 THEN

    I:= 1 (* Initialize I *) 

       I := '^/I.CV' + 1 ;

       J := '^/J.CV' + 1 ; (* Not Yet *)

       RANK := 16; (*Not Yet *)

    WHILE I <= 16 DO

             RANK := 16

             J := 1 (* Initialize J *)

      WHILE J <= 16 DO

    IF I <> J  (* Don't compare value against itself *)

      IF '^/FEILD_VALUE'Idea[1] >= '^/FEILD_VALUE'[J][1] THEN (* or equal to *)

    RANK := RANK-1

            END_IF;

    END_IF (*close the IF I added *)

                      J := J+1;

      END_WHILE;

            '^/FEILD_VALUE'Idea[2] := RANK;

                      I := I+1;

    END_WHILE ;

    ENDIF;

    The only problem so far is that if 2 numbers are identical, then the following rank will not exist, i.e. if 2 numbers are ranked 3rd because they are identical, then there will be no 4th ranked number...there will be a fifth ranked number. 

    You can probably handle that in the presentation logic through looping as well. 

  • In reply to Youssef.El-Bahtimy:

    Many Thanks  Youssef.El-Bahtimy for your modifications. The above code worked.

    Just a slight modification at :

    IF I <> J THEN

    Otherwise code works perfect for all unequal values.

    I am working on that part now.

    Really appreciate a lot for all your help. Smile

    Thank you very much.

  • In reply to Youssef.El-Bahtimy:

    I am getting a configuration error on the calc block when i rank more than 25 values .Any idea why.
    It worked flawlessly as long as the number of items is less than 26
  • In reply to Eliyya Shukeir:

    Check the WHILE loop in Books Online: there is a limit on how many iterations the loop will do. If you try to do more, the controller thinks it's an endless loop and stop execution of your CALC block.