Finding the highest number in a series of integer-based variables (in what i am sure is a terribly easy question)

hello!

i could bore you all with my various excuses, but quite frankly i am absolutely sure this is a question where i can either spend 5 hours wandering through the weeds getting increasingly confused, or somebody can come along and point me to the answer in 15 seconds or less and then we both have a good laugh about how i missed something so blatantly obvious. so please bear with me for a second on this one.

what i’m aiming to do is to more or less go

"hello computer. consider the following variables, which are all numbers we’ve been keeping track of going up and down this whole time: $alpha, $beta, $gamma and $delta. please tell me which of them is the highest number.

if $alpha is the highest number, set the variable $winner to alphawins. if $beta is the highest number, set variable $winner to betawins, etc."

i then know how to have it display different text for each - “if $winner’s value is alphawins, display this text; if $winner’s value is betawins display this text etc etc” - that’s easy enough to spot in the various cookbooks.

i am sure that given enough rubbing my two remaining brain cells together, i could create an absolute abomination of rickety code that would make intro to compsci professors around the world begin to scream. but i’m guessing that a) there has GOT to be a better way to do it than an extended jenga tower of “okay computer is $alpha greater than $beta, if not discard $alpha from the list, if $alpha is see if $alpha is greater than $gamma…”, b) if that really is the best way to do that in Harlowe, someone has probably written a macro function as to spare everyone from the sheer gory spectacle of making the computer do that level of busywork.

…and while computers have advanced to the point where making them do that level of busywork is comparatively very little to tax any CPU - i would imagine that you have to get way more complicated in Twine/Harlowe before you actually have to consider things like optimization - i would rather not create some code that makes actual programmers begin to spontaneously weep in despair when they are in its close proximity. in essence i am doing the programming equivalent of the first scene in an infomercial. all of these variables have just fallen over into my lap, so it’s time to look at the camera while shaking my head in despair and putting my hands on my hips because there’s got to be a better way!, so please someone come tell me about the as-seen-on-tv storage solution that will solve all my problems.

again, this is likely blatantly obvious - it’s probably something where it may be all i need is to be told the term that this function lives under in twine 2/harlowe! but i’ve definitely reached the “can’t see forest for the trees” level of brain scramble trying to find it. i wholeheartedly invite everyone to point out the super obvious thing i’ve missed while pointing and laughing at me for it.

thank you all so much in advance!

Try:

 $winner = Math.max($alpha, $beta, $gamma, $delta)
1 Like

I think in that case I’d just do something like (completely untested, sorry, so hopefully I haven’t screwed up the syntax):

(set: _highest to (max: $alpha, $beta, $gamma, $delta))
(set: _winner to (cond:
  _highest is $alpha, "alpha",
  _highest is $beta, "beta",
  _highest is $gamma, "gamma",
 _highest is $delta, "delta"))

Harlowe docs: (max:), (cond:)

If you have a ton of them, you could do something fancier like putting the variables and their associated texts into arrays but it’s pretty easy to copy/paste and change these lines when you add a new value, and it should be reasonably clear and readable when you come back to it months later and have forgotten what it does, so unless you have more than maybe 10-15 I’d probably just do it like this…

2 Likes

thank you both so very much!!

i… i think i need to go write a very heartfelt note of apology to _highest. if anyone needs me i’ll be in that poor neglected function’s dressing room, begging for forgiveness, with the absolute largest bouquet of flowers i can find

1 Like

Hey, just wanted to drop in to say welcome to the forum, and this actually covers a piece of a programming puzzle I was considering for Neo Twiny Jam. So your question helped someone else out too.

1 Like

Ah, the function is (max:) btw. _highest is just a temporary variable to store its output: you could call it _arglefraster if you wanted.

But nah, this stuff is tricky: it takes a certain mindset of wandering around looking at the problem from all different angles until you find a direction that it’s approachable from. And that’s weird if you haven’t spent years programming.

And it’s an odd blind spot that there isn’t a helper function to do this directly: it is something you want to do from time to time…