Forum Discussion

Opher_Shachar_6's avatar
Opher_Shachar_6
Icon for Nimbostratus rankNimbostratus
Feb 26, 2012

Looking for a terse expression to get the (base) name of a Virtual

Hello all,

 

Given that [virtual name] evaluates to "/Common/Lishka.app/Company_vip"

 

What is the most terse expression to split that into

 

"/Common/Lishka.app/" and "Company_vip"

 

I have this:

 

set vname "[string range [virtual name] [expr [string last / [virtual name]]+1] end]"

 

set vpath "[substr [virtual name] 0 $vname]"

 

Thanks,

 

Opher.

 

  • Hi Opher,

     

     

    Out of curiosity, why do you want to get just the virtual name?

     

     

    I think what you have is about as efficient as it would get if the folders could be more or less than two deep. If it's always two, you could use scan:

     

     

    scan {/Common/Lishka.app/Company_vip} {/%[^/]/%[^/]/%[^/]} a b vs

     

    set folder "/$a/$b/"

     

    log local0. "\$folder: $folder, \$vs: $vs"

     

     

    Or you could use a single regexp for any number of directories:

     

     

    regexp -all {(/.*/)(.*)} {/Common/Lishka.app/Company_vip} all folder vsname

     

    regexp -all {(/.*/)(.*)} {/Common/test/Lishka.app/Company_vip} all folder vsname

     

     

    But the scan is ~3x faster than the regex:

     

     

    % time {scan {/Common/Lishka.app/Company_vip} {/%[^/]/%[^/]/%[^/]} a b c; set folder /$a/$b/} 100

     

    3 microseconds per iteration

     

     

    % time {regexp -all {(/.*/)(.*)} {/Common/Lishka.app/Company_vip} all folder vsname} 100

     

    10 microseconds per iteration

     

     

    time {set vname "[string range "/Common/Lishka.app/Company_vip" [expr [string last / "/Common/Lishka.app/Company_vip"]+1] end]"} 100

     

    6 microseconds per iteration

     

     

     

    You could open a case with F5 Support to request an enhancement. Maybe we could add a -name and -folder flags or something like that to [virtual name] to get the name and folder.

     

     

    Aaron
  • Hi Opher,

    I think the string commands actually work best to parse a VS name with or without folder(s):

    
    when RULE_INIT {
    
    set vsnames [list "/dir1/dir2/myvs" \
    "/Common/myvs" \
    "myvs" \
    ]
    foreach vsname $vsnames {
    log local0. "$vsname parsed as [string range $vsname [expr {[string last "/" $vsname]+1}] end]"
    }
    }
    
     Logs:
    : /dir1/dir2/myvs parsed as myvs
    : /Common/myvs parsed as myvs
    : myvs parsed as myvs
    

    Aaron
  • Thanks Aaron,

    As DevCentral notification mail was hitting my "Spam mail", I've not seen your former post (from 02/29/2012) and just now seen your last post.

    I've actually came up with a terser form:

     set vname [URI::basename [virtual name]] 

    Don't know how fast (or slow) this is ... but it's more pleasant to the eye of my iRule unversed colleague who also supports the ltm.

    Regards,

    Opher.