Forum Discussion

Thomas_Laubrock's avatar
Thomas_Laubrock
Icon for Nimbostratus rankNimbostratus
Sep 05, 2005

foreach iterates an array in reverse order

Hi *,

 

 

I just fund a false friend.

 

A foreachstatement like:

 

 

array set ersetzer {

 

"https://portal.domain:80" "https://portal.domain.net"

 

"http://portal" "https://portal"

 

}

 

 

foreach {myfind myreplace} [array get ersetzer] {

 

set offset 0

 

set diff [expr [string length $myreplace] - [string length $myfind]]

 

Get indices of all instances of find string in the payload

 

set indices [regexp -all -inline -indices $myfind [HTTP::payload]]

 

log "Search for: $myfind, replace with $myreplace, found at indices"

 

[here same code that replaces the strings in the http payload]

 

}

 

 

runs trough the array in reverse order, so this code will first search for "http://portal" and then for "https://portal.domain.net:80"

 

 

Because I need the replacements in the right order it is very important, that you know the untypical behaviour for the foreach statement.

 

 

Hopefully the netx release will behave in the same way, otherwies ... goob luck and happy iRule debugging :-)

 

 

Bye

 

Thomas

3 Replies

  • Hi,

     

     

    is it possible that an array in tcl has not a defined order?

     

    Is there an easy to use ordert list or something like this?

     

     

    bye

     

    Thomas
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    In Tcl, a list is really an ordered array. An array is really a hash. So, the order in which an array is iterated across depends on the hash of the entries and would appear completely random. If you always want to iterated in a specific order, then you likely want a list instead.