Test if attribute value is within an array of values

  • Asked
  • Active
  • Viewed 2762 times
0

Dear OF Team,

Is there a way to do the following test more nicely/shorter: tree[species=1 or species=8 or species=10 or species=11 or species=12 or species=16 or species=19 or species=22 or species=23]

What I'm looking for is something like this (e.g. in R): tree[species %in% c(1,8,10,11,12,16,19,22,23)]

Could this be implemented if doesn't exist yet?

Thanks! Andras

1

Accepted answer

Hi András,
In the latest version of Collect (3.22.x) there is a new function idm:array that you could use in combination with idm:contains function to simplify your expression, that will become:

idm:contains(idm:array(1,8,10,11,12,16,19,22,23), species)

Many thanks,
Open Foris Team

Hi,

It seems there is a problem with the expression above. If I try to use it to get a subset of a group of items, it doesn't work:

count(parent()/tree[idm:contains(idm:array(1,8,10,11,12,16,19,22,23),species)])

Am I doing something wrong?

Andras Andras

The species code is an alphanumeric value, so it must be wrapped into single or double quotes. Try to use this expression instead: count(parent()/tree[idm:contains(idm:array('1','8','10','11','12','16','19','22','23'),species)])
However, I would split the expression into 2 separate calculated attributes, one attribute that says if a species in of a specific type and the other one that counts the trees of that species, and I would even move the count outside of the 'tree' entity, since it does not depend on the current tree item.

Stefano (OF) Stefano (OF) ♦♦

Your answer

Markdown text. Shortcuts: Ctrl+B bold, Ctrl+I italic, Ctrl+K link, Ctrl+E code, Ctrl+Shift+V paste as plain text. Tab indents list items and code; press Escape, then Tab, to leave the text box.

Insert an image

Say what the image shows, for people who can't see it.

Keyboard shortcuts

Anywhere

?
Show this list
/
Search
g h
Go to the home page
g q
Go to the questions
g t
Go to the tags
g u
Go to the users
a
Ask a question

Lists of questions

j k
Next or previous question
Enter
Open the question

Question pages

j k
Next or previous post

Shortcuts never apply while you type in a text field. Members can turn them off in their preferences.