Re: Build intersection of two comma separated lists?
Sergio Charrua via sr-users <[email protected]>
| Newsgroups | gmane.comp.voip.ser |
|---|---|
| Message-ID | <CALZWR5xyQdbOzNwqKyVmFdEBt36LDcFO3Wrx5r-t9=Z8+a-rTQ@mail.gmail.com> |
Salut Benoit!
you could use KEMI and Javascript. Here is my 2 cents (didn't try,
though...just streaming out of my head :) )
function get_codec_intersection(src, dst) {
const a = new Set(src.split(",").map(s => s.trim()).filter(Boolean));
const out = [];
dst.split(",").map(s => s.trim()).filter(Boolean).forEach(c => {
if (a.has(c)) out.push(c);
});
return out.join(",");
}
Or maybe straight Kamailio scripting (not tested neither... but I use
similar function/route block for other processing, just had to adapt it for
your use case)
route[GET_CODEC_INTERSECTION] {
# copy originals
$var(src) = $var(src_codecs);
$var(dst) = $var(dst_codecs);
//replace comma by semicolon, less risk of parsing failure
$var(src) = $(var(src){re.subst/,/;/g});
$var(dst) = $(var(dst){re.subst/,/;/g});
$var(out) = "";
$var(n) = $(var(src){param.count,;});
$var(i) = 0;
while($var(i) < $var(n)) {
# Get codec name at index i from the ';' list
$var(_c) = $(var(src){param.name,$var(i),;});
$var(_c) = $(var(_c){s.trim});
# If codec exists in dst list, add to output
if($(var(dst){param.in,$var(_c),;}) == 1) {
if($var(out) == "") {
$var(out) = $var(_c);
} else {
$var(out) = $var(out) + "," + $var(_c);
}
}
$var(i) = $var(i) + 1;
}
$dlg_var(codec_filter) = $var(out);
}
I would go for the KEMI version, even if there is a slight extra runtime
increase.
Atenciosamente / Kind Regards / Cordialement / Un saludo,
*Sérgio Charrua*
*www.kahea.ai <http://www.kahea.ai> / www.voip.pt <http://www.voip.pt>*
*OpenTelecom* - Consulting for Telecoms, Lda
Tel.: +351 <callto:+351+91+104+12+66>91 631 11 44
Email : *[email protected] <[email protected]>*
This message and any files or documents attached are strictly confidential
or otherwise legally protected.
It is intended only for the individual or entity named. If you are not the
named addressee or have received this email in error, please inform the
sender immediately, delete it from your system and do not copy or disclose
it or its contents or use it for any purpose. Please also note that
transmission cannot be guaranteed to be secure or error-free.
On Mon, Feb 16, 2026 at 2:51 PM Benoit Panizzon via sr-users <
[email protected]> wrote:
> > Is there anything like an intersection function? Is there an easy way
> > to build such a function with existing functionality?
>
> I came up with this, wondering if there is a more elegant solution:
>
> $var(i) = 0;
> $var(A) = "PCMA,AMR-WB,telephone-event";
> $var(B) = "G722,PCMA,G729,telephone-event";
> $var(inters) = "";
> $var(separator) = "";
> while ($(var(A){s.select,$var(i),,}) != "") {
> if (in_list("$var(A)","$var(B)",",")) {
> $var(inters) = $var(inters) + $var(separator) + $var(A);
> $var(separator) = ",";
> }
> $var(i) = $var(i) + 1;
> }
> xlog("L_INFO", "$cfg(route): XXXXXXX DEBUG: INTERSECITON of $var(A) and
> $var(B) is $var(inters) \n");
>
> Mit freundlichen Grüssen
>
> -Benoît Panizzon-
> --
> I m p r o W a r e A G - Leiter Commerce Kunden
> ______________________________________________________
>
> Zurlindenstrasse 29 Tel +41 61 826 93 00
> CH-4133 Pratteln Fax +41 61 826 93 01
> Schweiz Web http://www.imp.ch
> ______________________________________________________
> __________________________________________________________
> Kamailio - Users Mailing List - Non Commercial Discussions --
> [email protected]
> To unsubscribe send an email to [email protected]
> Important: keep the mailing list in the recipients, do not reply only to
> the sender!
>
__________________________________________________________
Kamailio - Users Mailing List - Non Commercial Discussions -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the sender!