[OpenSIPS-Users] best way to do "foreach"

Kingsley Tart kingsley at dns99.co.uk
Wed May 12 10:32:02 EST 2021


Is there a recommended way to implement a foreach loop with the
iterator iterating over a list of known values? 


For string values that won't themselves contain a comma, I could do
this:

    $var(valCSV) = "string1,string2,string3,string4";
    $var(valcount) = $(var(valCSV){csv.count});
    $var(valindex) = 0;

    while ($var($valindex) < $var($valcount)) {
        $var(thisval) = $(var(valCSV){csv.param,$var(valindex)});
        $var(valindex) = $var(valindex) + 1;
        # more code
    }


or I could use an AVP (also handy if the strings might contain a
comma):


    $avp(vals) = "string4";
    $avp(vals) = "string3";
    $avp(vals) = "string2";
    $avp(vals) = "string1";
    $var(valcount) = 4;

    $var(valindex) = 0;

    while ($var($valindex) < $var($valcount)) {
        $var(thisval) = $avp(vals);
        $avp(vals) = NULL;
        $var(valindex) = $var(valindex) + 1;
        # more code
    }

(specifically using an iterator to count through the loop in case
something else is already using that AVP - only want to delete what I
added in the first place).


Is there a perferred way, or better way than these?

Cheers,
Kingsley.




More information about the Users mailing list