A Harlowe 1.2.2 question.
So, I have a list of competitors for an election that I thought should be stored as a datamap of names, each name paired with a random value representing electability. Is there an easy way to get the name paired with the highest electability value, or better still (since Roman elections returned 2 winners) the names associated with the two highest electabily values? I've been throwing myself at it brute force without much luck. I'm pretty new to Harlowe but I've been hobby coding for a long time if that makes a difference. Any suggestions?
Comments
1. Iterating/looping through the members of a datamap.
You can't do this directly but you can use the (datanames: ) macro to obtain a sorted array containing the keys of each of the members which you can Iterate/loop through instead.
2. Harlowe currently has no loop (or equivalent) macro.
The following example uses a (live:) macro to simulate this functionality, this is only one of the possible methods that can be used and each method has it's problems.
note: The following example includes more $variables than are actually needed to solve the problem but I wanted to make the code as clear as possible, it also includes debugging output to show the current values of some of the variables used.
If any part of the above does not make sense then just ask for further clarification.
<code>
{
(set: $first to "")
(set: $firstVotes to 0)
(set: $second to "")
(set: $secondVotes to 0)
(set: $current to "")
(set: $currentVotes to 0)
(set: $names to (datanames: $aedileCompetitors))
(set: $length to $names's length)
(set: $index to 0)
(live: 10ms)[
(set: $index to it + 1)
(set: $current to $names's ($index))
(set: $currentVotes to $aedileCompetitors's ($current))
(if: $currentVotes > $firstVotes)[
(set: $second to $first)
(set: $secondVotes to $firstVotes)
(set: $first to $current)
(set: $firstVotes to $currentVotes)
] (else-if: $currentVotes > $secondVotes)[
(set: $second to $current)
(set: $secondVotes to $currentVotes)
]
(if: $index >= $length)[
(stop:)
Winners:
(print: "First: " + $first + ": " + (text: $firstVotes))
(print: "Second: " + $second + ": " + (text: $secondVotes))
]
]}
Winners:
(print: "First: " + $first + ": " + (text: $firstVotes))
(print: "Second: " + $second + ": " + (text: $secondVotes))
</code>
I have taken out the debugs and used my own datamap name of $aedileCompetitors to replace your $competitors. The algorithm works perfectly, but the values in $first, $firstVotes, $second, $secondVotes do not seem to last outside the loop. The winner variable display properly in the printout after (stop:). They are empty in the last printout at the end of the code here. Suggestions? I'm not quite able to use the values yet since they don't seem to have a lifespan past the loop. I must be missing something.
Thanks again.
And they are empty and 0 outside the loop. Grateful for any suggestions
One way to fix this is to add a (go-to:) macro after (stop:), the relevant variables will have the correct value within the next passage displayed.
Another answer is to change the method being used to simulate looping. The following example uses Recursion to achieve the same outcome and it consists of two passages.
1. The main passage where the process is started and where the outcome variables will be available.
2. The Recursive Logic passage, were the work is done.