# Selecting the highest of several variables?

So, this is something I’m pretty sure I can figure out a way around, but I wanted to ask here first because I want to be sure there’s not an out-of-the-box way to do it. In the project I am working on, I have a case where I’ve got a bunch of objects that all have a number variable, and I want to select the object with the lowest/highest of those, i.e. something like “pick the monster with the lowest energy”, “pick the item with the lowest price”. I also have a case where a single object has several number variables, and I’d like to be able to pick the highest/lowest of those, too, so that’s closely related, i.e., something like “pick Tom’s highest skill variable out of strength, intelligence, and juggling.”

Things I’ve thought of:

1. I could use a whole bunch of nested ifs, asking if one thing is larger than another. But that seems like a mess when making a large number of comparisons.
2. I could add these values to a list or table, sort, and select. That seems better, but since this might entail having something like A Table of Things Present In This Location, it seems like I am imagining something that’s pretty redundant with the world model, which makes me suspicious that there’s another approach I’m not aware of. (Also, I am somewhat unsure if table-sort will handle real numbers well; I seem to recall having some issue with that a long time ago.)

Am I overlooking/unaware of a simpler solution here, or are these the trees up which I should be barking?

There’s no built-in way. A table is easiest.

You’re right that there’s a table-sort bug involving real numbers (inform7.com/mantis/view.php?id=1555). If all of your numbers are non-negative, it won’t be a problem. If some of them are negative, you will have to do extra futzing. (I guess you could put everything in a table, and then iterate to find the highest entry, rather than relying on “sort”.)

Ah, cool, thank you! I think I can sneak by without negatives in this case, but this is excellent to know, as I’m pretty sure I’ve bumped into that problem before, and I might have not been able to guess what precisely was tripping things up.

You can also iterate through the values, keeping track of the largest/smallest value found so far. E.g.:[code]A thing has a number called price.

Place is a room. Foo is in place. The price is 5. Bar is in place. The price is 8. Baz is in place. The price is 2.

When play begins:
let highest-object be an object;
let highest-price be 0;
repeat with current-object running through things in Place:
if the price of current-object is greater than highest-price:
now highest-object is current-object;
now highest-price is the price of current-object;
say “Highest price: [highest-object].”.[/code](I use a separate highest-price variable [instead of highest-object’s built-in price property] to ensure that it always starts at 0.)

You could also write this as a fancy general-purpose routine that took a list of objects, e.g.:```To decide which object is the highest-priced item in (item-list - a list of objects): let highest-object be an object; let highest-price be 0; repeat with current-object running through item-list: if the price of current-object is greater than highest-price: now highest-object is current-object; now highest-price is the price of current-object; decide on highest-object.```

Or you can define an adjective.

``````A thing has a number called the price.
Definition: a thing is expensive if the price is at least 100.``````

Now you can talk about “the most expensive thing carried by the player”, or “if the wand is more expensive than the book”, or things like that. Inform deduces from the “at least” (greater-or-equal) in the definition that things with a higher price should be considered more expensive.