On Wed, 22 Dec 2021, Dovid Bender wrote:
> I am experimenting with arrays in Asterisk. I am looking
> at https://wiki.asterisk.org/wiki/display/AST/Function_SHIFT
> and https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+Function_ARRAY.
>
> So for example I Do Set(FOO(1,2,3,4)=10,20,30,40)
>
> What would be the correct way to get both the key and value into an
> array? I want to say for instance OPT=1 and then OPT_VAL=10. Then on the
> next interaction for OPT=2 and OPT_VAL=20 etc.
>
> Is this possible or am I looking at this wrong?
In my mind, Asterisk does not really have arrays. You can set channel
variables and pretend they are an array, but they are not an array like in
real programming languages -- like 'how many elements are in this array'
or 'throw an exception if I try to access an invalid subscript' or
'iterate over every element in this array.'
For example, you can set channel variables like:
same = n, set(foo1=1)
same = n, set(foo2=2)
same = n, set(foo3=3)
same = n, set(foo4=4)
or
same = n, set(ARRAY(foo1,foo2,foo3,foo4)=1,2,3,4)
or
same = n, mset(foo1=1,foo2=2,foo3=3,foo4=4)
and 'dumpchan()' will show 4 discrete variables that have the same 3
letters and no other relationship.
(You could also 'set(ARRAY(foo1,bar2,baz3,boo4)=1,2,3,4)' to see that
there is no difference from setting channel variables as discrete
'set()'
statements or using the 'ARRAY()' function or using the 'mset()'
application)
You can 'pretend' a variable is an array by concatenating a
'subscript'
like:
same = n, set(foo${key}=value)
or
same = n, set(foo-${key}=value) ; a little bit more readable
but 'dumpchan()' will show each 'element' as a discrete channel
variable.
Note that 'key' does not need to be numeric. It is just text that is
concatenated to form the channel variable name. This may be used to
pretend that Asterisk has associative arrays.
The 'SHIFT()' function just removes and returns the leading substring of
a
variable up to a delimiter. It has even less to do with arrays than
'ARRAY()' :)
--
Thanks in advance,
-------------------------------------------------------------------------
Steve Edwards sedwards at sedwards.com Voice: +1-760-468-3867 PST
https://www.linkedin.com/in/steve-edwards-4244281