For-Each Example

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

For-Each Example

Kneeoh
I'm trying to figure out how to iterate through an indexed $avp that holds json query results. The docs are kind of spotty. Can someone share a working snippet of code that iterates through an $avp ? This one isn't quite working for me. First off it says only $var and $avp can be iterated, but this example has a $json var.

# iterate through all JSON documents returned by a MongoDB query
    cache_raw_query("mongodb:location", "{... find ...}", "$avp(res)");
    for ($json(contact) in $(avp(res)[*]))
        xlog("Found: $json(contact/phone) $json(contact/email)\n");


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: For-Each Example

Bogdan-Andrei Iancu-2
Hi,

If you have an AVP holding multiple value (instances), you can iterate through all of them as:

$var(n) = 0;
while ( $(avp(my_avp)[$var(n)]) != NULL ) {
    xlog("value of my avp on position $var(n) is
$(avp(my_avp)[$var(n)]) \n");
    $var(n) = $var(n) + 1 ;
}

Regards,
Bogdan-Andrei Iancu
OpenSIPS Founder and Developer
http://www.opensips-solutions.com
On 09.09.2015 00:01, Kneeoh wrote:
I'm trying to figure out how to iterate through an indexed $avp that holds json query results. The docs are kind of spotty. Can someone share a working snippet of code that iterates through an $avp ? This one isn't quite working for me. First off it says only $var and $avp can be iterated, but this example has a $json var.

# iterate through all JSON documents returned by a MongoDB query
    cache_raw_query("mongodb:location", "{... find ...}", "$avp(res)");
    for ($json(contact) in $(avp(res)[*]))
        xlog("Found: $json(contact/phone) $json(contact/email)\n");



_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: For-Each Example

Liviu Chircu
In reply to this post by Kneeoh
Hi Kneeoh,

The $json(contact) iterators are only available for the upcoming OpenSIPS 2.2, or current master branch on GitHub [1], which is the version
specifically addressed by the documentation page you followed on opensips.org [2]

[1]: https://github.com/OpenSIPS/opensips
[2]: http://www.opensips.org/Documentation/Script-Statements-2-2

Best regards,
Liviu Chircu
OpenSIPS Developer
http://www.opensips-solutions.com
On 09.09.2015 00:01, Kneeoh wrote:
I'm trying to figure out how to iterate through an indexed $avp that holds json query results. The docs are kind of spotty. Can someone share a working snippet of code that iterates through an $avp ? This one isn't quite working for me. First off it says only $var and $avp can be iterated, but this example has a $json var.

# iterate through all JSON documents returned by a MongoDB query
    cache_raw_query("mongodb:location", "{... find ...}", "$avp(res)");
    for ($json(contact) in $(avp(res)[*]))
        xlog("Found: $json(contact/phone) $json(contact/email)\n");



_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users


_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: For-Each Example

Kneeoh
In reply to this post by Bogdan-Andrei Iancu-2
Thanks Bogdan, that did the trick.



On Wednesday, September 9, 2015 6:14 AM, Bogdan-Andrei Iancu <[hidden email]> wrote:


Hi,

If you have an AVP holding multiple value (instances), you can iterate through all of them as:

$var(n) = 0;
while ( $(avp(my_avp)[$var(n)]) != NULL ) {
    xlog("value of my avp on position $var(n) is
$(avp(my_avp)[$var(n)]) \n");
    $var(n) = $var(n) + 1 ;
}

Regards,
Bogdan-Andrei Iancu
OpenSIPS Founder and Developer
http://www.opensips-solutions.com
On 09.09.2015 00:01, Kneeoh wrote:
I'm trying to figure out how to iterate through an indexed $avp that holds json query results. The docs are kind of spotty. Can someone share a working snippet of code that iterates through an $avp ? This one isn't quite working for me. First off it says only $var and $avp can be iterated, but this example has a $json var.

# iterate through all JSON documents returned by a MongoDB query
    cache_raw_query("mongodb:location", "{... find ...}", "$avp(res)");
    for ($json(contact) in $(avp(res)[*]))
        xlog("Found: $json(contact/phone) $json(contact/email)\n");



_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users




_______________________________________________
Users mailing list
[hidden email]
http://lists.opensips.org/cgi-bin/mailman/listinfo/users