Contains function

Can the Contains function have a variable as the search criteria i.e.
{if: contains(list, variable)}

Yes, but you want the includes function to check a list. contains is for checking if a value is in a string. If you use contains with a list, it will convert the list to a string before checking it. Often that will work fine, but there are cases where you may get the wrong result.

Here is an example of includes:

{list=[ "abc", "xyz", "123" ] }
{formtext: name=to_check; default=abc}

Does list include "{=to_check}": {=includes(list, to_check)}

Thanks Scott but I am trying to use it in this test snippet and it doesn't find it. Maybe because the list is from a Repeat function?

Number of employees: {formmenu: 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; name=count}

{repeat: count; locals=employees}
Employee Name: {formtext: name=name} -- Hours worked: {formtext: name=hours; default=5}
{endrepeat}
Total hours worked: {=sum(map(employees, employee -> employee.hours))}
{repeat: for item in employees}
{=item} {=item.name} {=item.hours}
{endrepeat}

Employees: {=employees}
{formtext: name=search}
{=includes(employees,search)}

Hi,

I've edited your example above to print out the employees variable above the search. You can see it will look something like:

[["name": "Scott", "hours": "5"]]

The includes function only looks at the top level of the list, while the name is nested one level down in your data structure. (contains() mostly works here as the whole nested list is flattened to one string).

If you wanted to check for the name, you could do something like this:

includes(map(employees, e -> e.name), "Scott")

Thanks Scott. The idea was that the search criteria would not need to be hard-coded but a variable as in the snippet

You can use a variable instead of "Scott".

For example, if you have a variable search_name, this will work:

includes(map(employees, e -> e.name), search_name)

That's great, thank you Scott