discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

is there a function to return if 'x' is in a list?

JD
John David
Thu, Mar 6, 2025 3:40 PM

I wanted to set up  "skip" function, but giving it a list of indexes to
skip.

Does openscad have the equivalent of "if 'x' in mylist", or do I need to
implement one.  A quick google search did not turn up anything...

EBo --

I wanted to set up "skip" function, but giving it a list of indexes to skip. Does openscad have the equivalent of "if 'x' in mylist", or do I need to implement one. A quick google search did not turn up anything... EBo --
AM
Adrian Mariano
Thu, Mar 6, 2025 3:54 PM

The search function does that: search([x],mylist). It is a bit quirky.
The BOSL2 function list_remove may do what  you want.

On Thu, Mar 6, 2025 at 10:40 John David via Discuss <
discuss@lists.openscad.org> wrote:

I wanted to set up  "skip" function, but giving it a list of indexes to
skip.

Does openscad have the equivalent of "if 'x' in mylist", or do I need to
implement one.  A quick google search did not turn up anything...

EBo --


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

The search function does that: search([x],mylist). It is a bit quirky. The BOSL2 function list_remove may do what you want. On Thu, Mar 6, 2025 at 10:40 John David via Discuss < discuss@lists.openscad.org> wrote: > I wanted to set up "skip" function, but giving it a list of indexes to > skip. > > Does openscad have the equivalent of "if 'x' in mylist", or do I need to > implement one. A quick google search did not turn up anything... > > EBo -- > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RW
Raymond West
Thu, Mar 6, 2025 5:51 PM

Hi Adrian, in what way is search quirky?

I've a couple of functions that may do what is requested.

oldlist = [1,2,3,4,5,6,7,8,9,10];
skip = [3,9,5,7];  // should give 1,2,3,5,7,9

//filter a list by skipping indexes
function filter(oldlist, skip) = [
    for (i =[ 0:1:len(oldlist)-1])
        (search(i, skip) == []) ? oldlist[i] : []
];

// remove empty entries
function flatten(list) = [
    for (x = list) each (x == [] ? [] : [x])
];

newlist = flatten(filter(oldlist, skip));
echo(oldlist);
echo(newlist);

On 06/03/2025 15:54, Adrian Mariano via Discuss wrote:

The search function does that: search([x],mylist). It is a bit quirky.
  The BOSL2 function list_remove may do what  you want.

 On Thu, Mar 6, 2025 at 10:40 John David via Discuss
discuss@lists.openscad.org wrote:

 I wanted to set up  "skip" function, but giving it a list of
 indexes to skip.

 Does openscad have the equivalent of "if 'x' in mylist", or do I
 need to implement one.  A quick google search did not turn up
 anything...

   EBo --
 _______________________________________________
 OpenSCAD mailing list
 To unsubscribe send an email to discuss-leave@lists.openscad.org

OpenSCAD mailing list
To unsubscribe send an email todiscuss-leave@lists.openscad.org

Hi Adrian, in what way is search quirky? I've a couple of functions that may do what is requested. oldlist = [1,2,3,4,5,6,7,8,9,10]; skip = [3,9,5,7];  // should give 1,2,3,5,7,9 //filter a list by skipping indexes function filter(oldlist, skip) = [     for (i =[ 0:1:len(oldlist)-1])         (search(i, skip) == []) ? oldlist[i] : [] ]; // remove empty entries function flatten(list) = [     for (x = list) each (x == [] ? [] : [x]) ]; newlist = flatten(filter(oldlist, skip)); echo(oldlist); echo(newlist); On 06/03/2025 15:54, Adrian Mariano via Discuss wrote: > The search function does that: search([x],mylist). It is a bit quirky. >   The BOSL2 function list_remove may do what  you want. > >  On Thu, Mar 6, 2025 at 10:40 John David via Discuss > <discuss@lists.openscad.org> wrote: > > I wanted to set up  "skip" function, but giving it a list of > indexes to skip. > > Does openscad have the equivalent of "if 'x' in mylist", or do I > need to implement one.  A quick google search did not turn up > anything... > >   EBo -- > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email todiscuss-leave@lists.openscad.org
JB
Jordan Brown
Thu, Mar 6, 2025 6:11 PM

On 3/6/2025 9:51 AM, Raymond West via Discuss wrote:

Hi Adrian, in what way is search quirky?

It looks like it's not too bad for a simple lookup of a number in a list
of numbers.

Try doing anything with strings.

Hint:  search("b", ["a", "b", "c"]) doesn't work.

On 3/6/2025 9:51 AM, Raymond West via Discuss wrote: > > Hi Adrian, in what way is search quirky? > It looks like it's not too bad for a simple lookup of a number in a list of numbers. Try doing anything with strings. Hint:  search("b", ["a", "b", "c"]) doesn't work.
R
Rudolf
Thu, Mar 6, 2025 11:10 PM

echo(search(["a"], ["b","H","a"]));

works well.

Am 06.03.2025 um 19:11 schrieb Jordan Brown via Discuss:

On 3/6/2025 9:51 AM, Raymond West via Discuss wrote:

Hi Adrian, in what way is search quirky?

It looks like it's not too bad for a simple lookup of a number in a
list of numbers.

Try doing anything with strings.

Hint:  search("b", ["a", "b", "c"]) doesn't work.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

echo(search(["a"], ["b","H","a"])); works well. Am 06.03.2025 um 19:11 schrieb Jordan Brown via Discuss: > On 3/6/2025 9:51 AM, Raymond West via Discuss wrote: >> >> Hi Adrian, in what way is search quirky? >> > > It looks like it's not too bad for a simple lookup of a number in a > list of numbers. > > Try doing anything with strings. > > Hint:  search("b", ["a", "b", "c"]) doesn't work. > > > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
JB
Jordan Brown
Fri, Mar 7, 2025 1:08 AM

On 3/6/2025 3:10 PM, Rudolf via Discuss wrote:

echo(search(["a"], ["b","H","a"]));

works well.

Indeed.  But the need for the first string to be in an array isn't
exactly obvious.  Offhand, I remember that that works, but I don't
remember why it works.

That's not a question.  I can go reread the docs if I'm curious.  But
the fact that an obvious request - find a string in a list of strings -
has an unobvious answer, qualifies as "quirky".

On 3/6/2025 3:10 PM, Rudolf via Discuss wrote: > echo(search(["a"], ["b","H","a"])); > > works well. Indeed.  But the need for the first string to be in an array isn't exactly obvious.  Offhand, I remember that that works, but I don't remember *why* it works. That's not a question.  I can go reread the docs if I'm curious.  But the fact that an obvious request - find a string in a list of strings - has an unobvious answer, qualifies as "quirky".
JD
John David
Fri, Mar 7, 2025 1:57 AM

Thank you, Adrian, list_remove_values() solved my need in a slightly
different, and more elegant way.

On Thu, Mar 6, 2025 at 8:08 PM Jordan Brown via Discuss <
discuss@lists.openscad.org> wrote:

On 3/6/2025 3:10 PM, Rudolf via Discuss wrote:

echo(search(["a"], ["b","H","a"]));

works well.

Indeed.  But the need for the first string to be in an array isn't exactly
obvious.  Offhand, I remember that that works, but I don't remember why
it works.

That's not a question.  I can go reread the docs if I'm curious.  But the
fact that an obvious request - find a string in a list of strings - has an
unobvious answer, qualifies as "quirky".


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Thank you, Adrian, list_remove_values() solved my need in a slightly different, and more elegant way. On Thu, Mar 6, 2025 at 8:08 PM Jordan Brown via Discuss < discuss@lists.openscad.org> wrote: > On 3/6/2025 3:10 PM, Rudolf via Discuss wrote: > > echo(search(["a"], ["b","H","a"])); > > works well. > > > Indeed. But the need for the first string to be in an array isn't exactly > obvious. Offhand, I remember that that works, but I don't remember *why* > it works. > > That's not a question. I can go reread the docs if I'm curious. But the > fact that an obvious request - find a string in a list of strings - has an > unobvious answer, qualifies as "quirky". > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >