5489
« on: May 02, 2007, 09:29:39 AM »
The beta version is available in the "Beta Testing" forum.
You want to use the new switch() function to simplify your formula. The switch works like this: The first argument is the "switch" argument, meaning the result of the function will be chosen based on the value of that argument. The remaining arguments come in pairs. The first of each pair is the value to which to compare the switch argument, and the second of each pair is the result if the first argument and the switch argument match. You can also have a final "default" argument, which will be the result if the switch argument matches none of the comparison arguments. Sounds complicated, but it's not.
Let's say you have want to award points like this:
1st place: 50
2nd: 35
3rd: 25
4th: 20
5th: 15
6th: 13
7th: 12
8th: 11
9th: 10
10th and above: 9
Using if() functions, it would be something like:
if(r == 1, 50, if(r == 2, 35, if(r == 3, 25, if(r == 4, 20, if(r == 5, 15, .... <etc> )))))
or
if(r == 1, 50, 0) + if(r == 2, 35, 0) + if(r == 3, 25, 0) + if(r == 4, 20, 0) + ... <etc>
Using a switch() function is much easier:
switch(r, 1, 50, 2, 35, 3, 25, 4, 20, 5, 15, 6, 13, 7, 12, 8, 11, 9, 10, 9)
The first argument, r is the switch argument. We're "switching" on the value of r (rank). The next two arguments, 1 and 50, are the first pair. 1 is compared to the switch argument, and if it matches, the value 50 is returned. If it doesn't, it moves on to the next pair of arguments. 2 is compared to the switch argument, and if it matches, the value 35 is returned. If it doesn't, it moves on to the next pair of arguments. And so on. Notice that there are 9 pairs, and the final pair is "9, 10". Then there is an additional argument, "9". This is the default argument. If the switch argument doesn't match anything, the final default argument is returned. As you can deduce, if the rank is greater than 9, the final argument (9) will be returned. If the default argument is omitted, 0 is returned.
If it makes it easier, write it like this:
switch(r,
1, 50,
2, 35,
3, 25,
4, 20,
5, 15,
6, 13,
7, 12,
8, 11,
9, 10,
9)