-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to check if an array contains a value? #1037
Comments
There is no shorter way. But +1 for set matching. And -1 for the syntax proposed. IMHO we should move to the direction of dict-based syntax for expressions. |
Thanks for the answer, even if it isn't what I had hoped for... |
As @KOLANICH points out, there is no dedicated built-in way in the expression language other than the If we will ever implement this feature, it would be nice to make it work for ranges (which don't exist in Kaitai Struct yet, but were proposed in #130) and perhaps enums (#518), too. Currently we have only However, with a bit of ingenuity, you can actually implement the list inclusion operation yourself, but I'll let you decide for yourself whether this counts as a "shorter" way: seq:
- id: match_steps
type: 'match_step(known_values[_index], _index != 0 ? match_steps[_index - 1].is_known : false)'
repeat: expr
repeat-expr: known_values.size
instances:
a:
value: 42
known_values:
value: '[1, 2, 42, 46]'
is_a_in_known_values:
value: match_steps.last.is_known
types:
match_step:
-webide-representation: '{is_known}'
params:
- id: cur_known_value
type: s4
- id: prev_is_known
type: bool
instances:
is_known:
value: prev_is_known or _parent.a == cur_known_value Result (see https://ide.kaitai.io/):
|
What is the allowable syntax for conditional expressions?
I have to test if a field is equal to any one of 63 values. The only way I can think to do this is the following, where x1, x2, x3 etc. are integers and a is the field id.
There has to be a shorter way. Maybe something like list inclusion?
The text was updated successfully, but these errors were encountered: