Forum Discussion

Juha__Ranta_106's avatar
Juha__Ranta_106
Icon for Nimbostratus rankNimbostratus
Jun 28, 2005

How to iterate over DataGroup List elements ?

Hi,

 

 

How can I iterate / access elements of a datagroup ?

 

I have following 'string data group list' called 'my_str_class':

 

 

--- /config/bigip.conf ---

 

...

 

 

class my_str_class {

 

"http://server-a"

 

"http://server-b"

 

}

 

 

...

 

 

--- /config/bigip.conf ----

 

 

I want to iterate over this data group list, but iRule:

 

 

...

 

foreach i $::my_str_class {

 

do something with each element $i

 

(http-response redirect 'Location:' -header rewrite ..)

 

}

 

...

 

 

gives the following Tcl error msg:

 

 

- list element in braces followed by "" instead of space

 

 

If I print the data group with cmd 'log LOCAL0.debug $::my_str_class'

 

it looks like this:

 

 

{http://server-a} {http://server-b}

 

 

---

 

 

Could you Please include more examples to BIG-IP 9 iRule documentation.

 

 

PS: documentation for 'matchclass' is also missing, although I think

 

figured out how to use it.

 

 

 

  • I didn't write the logic behind the data groups but it seems that it is internally represented by a string. By adding the TCL split command to create a true TCL list, foreach should work. I'll verify if this is something we need to fix or if it's by design.

    ...  
       foreach i [split $::my_str_class] {  
         log local0. $i  
       }  
       ...

    As for examples, we're working on that. As for the usage of matchclass, do a search in the forums and you'll find plenty of examples. There is also an example in chapter 13 (Writing iRules) of the BIG-IP v9 user reference.

    -Joe

  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Thank you for pointing this out. A problem was discovered in accessing a class/DataGroup as a Tcl list.

     

     

    We have created CR49978 to address the problem and it has been fixed in the release scheduled for later this fall. If you require this to work then you may want to contact support to request a hotfix for the 9.1 release.

     

     

    BTW, I would suggest you try to use findclass or matchclass for your application as the performance of searching the class using those commands is significantly better than searching a Tcl list (even using lsearch). This is because the internal representation of the class/DataGroup is a very efficient hash structure. Of course, if your class is very small then the performance of the search would not be as relevant.

     

     

    Also, as a sidenote, once you reference a class/DataGroup as a Tcl list, the internal representation is actually converted into a Tcl list (this is where the bug was) and the performance gained if you had exclusively used findclass or matchclass is lost until the next time you reload the configuration.

     

  • Could you implement a version of matchclass that would return the matching

    value in 'class' instead returning numeric value (0, 1, ..)?

    I would like to know which one of the datagrouplist (or class) elements matches and do some extra manipulations using that information..

    --- Here is the doc for findclass ---

    findclass

    The findclass command searches a data group list for a member that starts with and returns the data-group member string. This is similar to the matchclass command, except that the member is not required to be equal; instead, the member is only required to start with the string and the command returns the entire member value.

    The syntax of the findclass command is:

    findclass [(separator)]

    --------------------------------------

    Correct me if I'm wrong, but I guess that in this case I cannot use 'findclass' command because the first argument (string) is 'longer' and the second argument (data group) contains the 'prefixes' that I need to match against.

    It seems to me that commands..

         
         set location [HTTP::header "Location"];     
         set matchidx [matchclass $location starts_with $::my_str_class];     
       

    ..set variable '$matchidx' to index of matching element in my

    datagroup list (class).

    Is there some way to get the value of element $matchidx from class

    or is it possible to make 'matchclass' return the value of matching

    element (instead of index). ?

    Thanks in advance, Juha

    --

    PS: Please put more effort on writing a complete iRule documentation.

  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Could you implement a version of matchclass that would return the matching value in 'class' instead returning numeric value (0, 1, ..)?

     

    Matchclass actually already does this. It returns 0 if no match was found, otherwise it returns the index (base 1) of the element that matched. However, as you have already realized, there really isn't an effecient means to get that value without converting the class into a Tcl list (which is currently broken and has the side effect of destroying the efficiency of the class match).

     

     

    At this point, I have created a new CR 50048 to add a new Tcl command for accessing an indexed element of a class while preserving its internal representation. This should help provide the missing piece you need. Additionally, we have CR34234 which would allow iRules to modify the contents of a class (eg, add new elements, delete elements, etc). However, I can't make any guarantees as to when these CR's will be implemented (though CR50048 is rather trivial and would likely make the next release).

     

     

    Thanks for your suggestions, it really helps to improve the usability of our iRules.

     

     

    As for your comment: PS: Please put more effort on writing a complete iRule documentation.

     

    We are painfully aware of this point. However, I am only a developer with a very little time to spare. I will certainly pass your feedback on to the appropriate people. I'm sure they have already gotten the point though. We did create this forum in hopes that it would help mitigate this problem.

     

  • This is an old thread and perhaps the information is out of date (I hope it is).

     

     

    Is there still an issue that referencing a class/DataGroup as a Tcl list causes a conversion to a list resulting in performance loss (no longer hashed in memory) until a config reload is done?

     

     

    unRuleY stated on 06/28/2005 9:50 PM :::::

     

     

    Also, as a sidenote, once you reference a class/DataGroup as a Tcl list, the internal representation is actually converted into a Tcl list (this is where the bug was) and the performance gained if you had exclusively used findclass or matchclass is lost until the next time you reload the configuration.