You can find the highest of a group of numbers using an array and the (for:) macro:
(set: $array to (a: 3, 6, 42, 8, 27))
(set: $highest to -1)
(for: each _item, ...$array)[
(if: _item > $highest)[
(set: $highest to _item)
]
]
To find the name associated with the highest value, you'll need to do a little more work, but the general idea is the same. Specifically, you'll want to:
- Create a data map that associates a value with a name.
- Split the map into two arrays, one containing the values, one containing the names.
- Use a counter to find the index in the array of values and use that index to get the name from the other array.
A finished chunk of code that does what you ask might look something like this:
{
<!-- set our values -->
(set: $cat to 3)
(set: $dog to 9)
(set: $onion to 2)
(set: $ilikecheese to 6)
(set: $zebra to '')
<!-- a few more variables we'll need later -->
(set: $count to 0)
(set: $highest to -1)
(set: $index to 0)
<!-- create our data structure -->
(set: $test to (dm:
'cat', $cat,
'dog', $dog,
'onion', $onion,
'ilikecheese', $ilikecheese
))
<!-- split it into two arrays -->
(set: $testNames to (datanames: $test))
(set: $testValues to (datavalues: $test))
<!-- run the test -->
(for: each _item, ...$testValues)[
<!-- count each loop -->
(set: $count to it + 1)
(if: _item > $highest)[
(set: $highest to _item)
<!-- use our counter to record the index for our arrays -->
(set: $index to $count)
(set: $zebra to $testNames's ($index))
]
]
}
$zebra: $highest
There might be a better or easier way, but if there is, I don't know it.