Summary:ASTERISK-00222: Application "VarLength"
Reporter:John Todd (jtodd)Labels:
Date Opened:2003-09-04 19:44:58Date Closed:2011-06-07 14:04:43
Versions:Frequency of
Description:It is sometimes the case that I need to determine how many characters long a particular ${STRING} is for purposes of chopping up different parts of the string.  An application is the best way to do this, I think.


A quick application that would allow me to determine the number of characters in a string would be a great help for doing conditional branching.  Perhaps VarLength(${STRING}|${LENGTH})  where ${STRING} is the string to be measured and ${LENGTH} is a user-definable string that would have the stored results?  

This can be done, of course, with extremely lengthy, iterative, and ugly diaplans using wildcards in exten matching, but it seems easier to do it in some cleaner fashion.  Additionally, I could use ugly AGI or System calls to do it, but that is somehow not as appealing or clean as an application, and it seems a bit like the long way to solve the problem.
Comments:By: James Golovich (jamesgolovich) 2003-09-16 23:43:47

Although its not documented in the README.variables like it should be.  There is a substituted "variable" called LEN( )
You could get the same results that you want by using something like:
Then ${LENGTH} would contain the length of ${STRING}

By: John Todd (jtodd) 2003-09-17 00:29:44

Hmmm... thanks, yep, didn't notice that before, I see it there in the source code in pbx.c, but here's my test:

exten => 2226,1,SetVar(FOO=987654)
exten => 2226,2,SetVar(BAR=LEN(${FOO}))
exten => 2226,3,Hangup

   -- Executing SetVar("SIP/2203-40b9", "FOO=987654") in new stack
   -- Executing SetVar("SIP/2203-40b9", "BAR=LEN(987654)") in new stack
   -- Executing Hangup("SIP/2203-40b9", "") in new stack

As you can see, it doesn't work as anticipated.  That should say (if it works as advertised)  "BAR=6".  I tried with a variety of commas and other separations (duplicating your example exactly, actually) and came up with the same results.  Does your version work correctly?

By: James Golovich (jamesgolovich) 2003-09-17 00:41:03

Oops I guess I should test it before saying.
Heres a working example that I used
exten => *39,1,SetVar(TEST=${LEN(12345)})
exten => *39,2,System(echo ${TEST} >>/tmp/variables.log)

In order to make sure that it worked with nested variables I also tried:
exten => *39,1,SetVar(TEST=${LEN(${CALLERID})})

By: John Todd (jtodd) 2003-09-17 01:16:05

OK, great, that did the trick.  I wasn't treating it as a variable offset with ${}  Thanks!

By: John Todd (jtodd) 2003-09-17 01:21:38

Added documentation bug: http://bugs.digium.com/bug_view_page.php?bug_id=0000284