Compare
Compare two data values as numbers, text or lists and send true or false: ordering, equality, contains, starts or ends with, pattern match, empty, in list.
- Data
- Since 0.2.0
- compare
- equal
- contains
- match
- regex
- condition
- boolean
- data
Data Compare tests two data values and sends true or false.
What it does#
Operation chooses the test. The first six, Greater, Less, Greater or equal, Less or equal, Equal and Not equal, match signal Compare. They compare as numbers when both values read as numbers, and otherwise as text, character by character. Equal on two lists is true when every element is equal, in order.
The rest work on text and lists. Contains is true when text A contains text B, or when list A contains B. Starts with and Ends with test the start and end of text A. Matches reads B as a regular expression and tests text A. Is empty is true for empty text, text that is only spaces, an empty list and no value; numbers and booleans are never empty. In list is true when list B contains A.
Case decides whether text tests care about upper and lower case. Compare runs whenever an input receives a value. An invalid regular expression sends no value and the inspector shows the error.
When to use it#
Use it to make decisions on data: check a setting against a limit, test whether a name contains a word, or see whether a note is in a list. For audio and control signals use signal Compare.
Ports#
| Port | Direction | Signal | Lanes |
|---|---|---|---|
a | Input | Data (any) | 1 |
b | Input | Data (any) | 1 |
result | Output | Data (boolean) | 1 |
Parameters#
| Parameter | Label | Range | Default | Unit | Scale | Notes |
|---|---|---|---|---|---|---|
op | Operation | 0 to 11 | 0 | linear | Comparison of a against b. Choices: 0 Greater, 1 Less, 2 Greater or equal, 3 Less or equal, 4 Equal, 5 Not equal, 6 Contains, 7 Starts with, 8 Ends with, 9 Matches, 10 Is empty, 11 In list. | |
epsilon | Tolerance | 0 to 1 | 0 | linear | Numeric difference still counted as equal. Shown when op is 4 or 5. | |
case | Case | 0 to 1 | 0 | linear | Whether text comparisons respect letter case. Choices: 0 Sensitive, 1 Insensitive. Shown when op is 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9. |
Operation (op)#
The test, a discrete choice. Values 0 to 5 have the same names and order as signal Compare.
Tolerance (epsilon)#
For Equal and Not equal on numbers, how far apart A and B may be and still count as equal, from 0 to 1. 0 asks for exact equality. Useful when a number comes from arithmetic, where 0.1 + 0.2 is not exactly 0.3.
Case (case)#
Sensitive or Insensitive, a discrete choice. Insensitive folds case for text ordering, equality, Contains, Starts with and Ends with, and adds the i flag to Matches.
Tips#
- To check a threshold, compare a number with Greater; the result is a boolean you can send on to other data nodes.
- Matches uses JavaScript-style regular expressions:
^kmeans starts with k,[0-9]+means one or more digits. - B has no text field of its own; connect a Value to B to compare with a fixed value.
See also#
Value, String, List, Compare (signal).
Help patches
Each help patch is an ordinary Toy that teaches one idea. The diagram shows the Patch; the preview shows its Frame. Open it in TuneToys to play and edit it.
1. Compare two values
beginner Teaches: op, epsilon
Two numbers compared, with a tolerance for Equal.
Connections as text
- Oscillator audio output connected to Patch outputs audio input
- Value “left” value output connected to Compare a input
- Value “right” value output connected to Compare b input
- A is 0.3 and B is 0.31. Equal is true while Tolerance is at least 0.01.
- Greater, Less and the rest compare numerically because both values are numbers.
- Data cables carry values, not sound. Select a data node to see its newest value in the inspector; the tone only keeps the Toy audible.
2. Match text
intermediate Teaches: op, case
Text checked for a word, with and without case folding.
Connections as text
- Oscillator audio output connected to Patch outputs audio input
- Value “name” value output connected to Compare a input
- Value “word” value output connected to Compare b input
- A is "Kick drum, round" and B is "kick". Contains is true only when Case is Insensitive.
- Matches reads B as a regular expression: try changing B to ^k.*d$ in the inspector.
- Data cables carry values, not sound. Select a data node to see its newest value in the inspector; the tone only keeps the Toy audible.
Related nodes
- Value A literal source, the data domain's number box and message box.
- String Text operations on data values: length, case, trim, split, replace, slice, pad, repeat, search, regular expressions, format and JSON.
- Compare Turn any signal into a gate or trigger by comparing it to a threshold or to another signal, lane by lane, with hysteresis and tolerance.