Forum Discussion

Frank_J_104756's avatar
Frank_J_104756
Historic F5 Account
Nov 04, 2009

comparing variable values

I have 4 counter variables that are set. I've incremented them based on the of times certain strings are seen in an array. I now need to compare these 4 different variables so that I can find out which has the lowest value

 

foreach lbentry [array names ::sessionlbarray] {

 

switch $lbentry {

 

$server1ip {set server1lb [expr {$server1lb} + 1]}

 

$server2ip {set server2lb [expr {$server2lb} + 1]}

 

$server3ip {set server3lb [expr {$server3lb} + 1]}

 

$server4ip {set server4lb [expr {$server4lb} + 1]}

 

}

 

}

 

what is the easiest way to compare the variables server1lb, server2lb, server3lb, and server4lb so that I can find the one w/ the lowest value ?

9 Replies

  • On the top of my head you can do the following:

     

     

     

    set winner [lindex [lsort -integer [{ $server1lb $server2lb $server3lb $server4lb }]] 1]

     

     

    I haven't tested this but I think this method might work.

     

     

    CB

     

     

     

     

  • Frank_J_104756's avatar
    Frank_J_104756
    Historic F5 Account
    Thank you CB...I'll give this a shot...it's looks like it'll do exactly what I want...compare the value but give me the variable name I want.
  • Frank_J_104756's avatar
    Frank_J_104756
    Historic F5 Account
    line 49: [undefined procedure: $server1lb] [{$server1lb} {$server2lb} {$server3lb} {$server4lb}]
  • I am not sure about this but another one that popped in my head is the following:

     

     

    expr {min({ $server1lb $server2lb $server3lb $server4lb }}

     

     

     

    CB

     

     

  • Frank_J_104756's avatar
    Frank_J_104756
    Historic F5 Account
    What I got to pass the syntax check and what makes sense to me is

     

    set winner [lindex [lsort -integer { $server1lb $server2lb $server3lb $server4lb }] 1]

     

     

    i think will use the array I set up w/ and the variable winner to extract the correct value. I'll know tomorrow when we start testing.

     

     

    Thanks again CB.
  • Hey CB, I think max and min were only added to TCL in v8.5 (iRules use 8.4 currently). Also, Frank, do you want to get the lowest value or to know which variable name has the lowest value? I think the examples so far will return the value--not the variable name.

     

     

    Aaron
  • Frank_J_104756's avatar
    Frank_J_104756
    Historic F5 Account
    Aaron, initially I wanted the variable name w/ the lowest value but actually gettng the value will work since i'm usnig the values as the index in the array. Using the value I can retrieve the correct associated variable
  • Also, make sure to use lindex 0 to get the lowest value as TCL lists are 0 indexed.

     
     when RULE_INIT { 
      
        set var0 4 
        set var1 3 
        set var2 2 
        set var3 1 
        set var4 0 
         
        log local0. "Sorted: [lsort -integer [list $var0 $var1 $var2 $var3 $var4]]\ 
           -> lowest: [lindex [lsort -integer [list $var0 $var1 $var2 $var3 $var4]] 0] " 
     } 
     

    Log output:

    : Sorted: 0 1 2 3 4 -> lowest: 0

    Aaron
  • Frank_J_104756's avatar
    Frank_J_104756
    Historic F5 Account
    thx for mentioning the 0 index Aaron, I'd have been scratching my head over that !!

     

    then I can use winner to retrieve the correct value of the array which is using the variables above as indexes

     

     

    array set counterarray {

     

    $server1lb$server1ip

     

    $server2lb$server2ip

     

    $server3lb$server3ip

     

    $server4lb$server4ip

     

    }

     

    set winner [lindex [lsort -integer { $server1lb $server2lb $server3lb $server4lb }] 0]

     

    LB::select $counterarray($winner)