Overcomplicated Multireplace

I found a niche usage of the multireplace function using math:
@{(round((var1 - 50)/(x))+1) This is sample text the first.|I am the second sample text.|Behold, a third sample text.}

It takes var1 as an integer and does funky stuff until it ends up being 1, 2, or 3. The variable x affects the threshold of when it will become one of the readable numbers.

If x = 50, var1 being below 25 triggers the first sample text and if var1 is at least 75, it triggers the third sample text. Anything in between triggers the second sample text.

The variable x changes the trigger numbers by at a rate of 1:0.5 from the base number 50. So if x = 30, the trigger numbers will be 35 and 65. (You still shouldn’t divide by 0.)

This is a very complicated version of using the different if’s and else’s:

*if var1 < (50-(x/2))
    This is a sample text the first.
*elseif var1 >= (50+(x/2))
     Behold, a third sample text.
*else
    I am the second sample text.

I would not recommend using this alternative. No, really. I just thought it was neat.

13 Likes

Definitely neat, but considering I scored a 17 in the Math section on my first ACT test a few years back, I think I’ll stick to the normal conventions here. :joy:

3 Likes

The idea is pretty neat, but I think the logic is a little funky.

This is not true, (round((var1 - 50)/(x))+1) can still end up being 0, which will cause an error in the multireplace.

For example, if x = 50 and var1 = 24, then:

  • var1 - 50 = 24 - 50 = - 26
  • (- 26) / 50 = - 0.52
  • round(- 0.52) = - 1
  • (- 1) + 1 = 0
  • var1 - 50 = 75 - 50 = 25
  • 25 / 50 = 0.5
  • round( 0.5 ) = 1
  • 1 + 1 = 2

This will trigger the second option.

You either didn’t notice that multireplace indices start at 1 and not 0, different than an array, or you forgot to check the lower limits for each condition.

For now, I think you can fix it (I could be wrong, it’s 3 a.m. here :sweat_smile::sleeping:) if you use 2 instead of 1

@{(round( (var - 50) / 50 ) + 2 ) One|Two|Three}

But I don’t think it’ll work for all divisors. This is an interesting challenge, though. I’ll try to work a solution as well :stuck_out_tongue_winking_eye:.

1 Like

Nice. This sort of construct should really get more explicit syntax, though. Coding all the logic inside a single parentheses pair is prone to error, as @cup_half_empty pointed out.

I’ve wished CS had some proper select case statements, as well; multireplace somewhat takes the place of them, but not entirely, and not across multiple lines.

1 Like

I have a halfass solution for this, but it’s not so cool as @StorybookParagon and it’s still liable to break.

@{ (round( ((x * n) / 100) + 0.499 )) One | Two | … | Umpteen}

Where:

  • x is the input variable (with a value between 0.1 and 100), and
  • n is the number of elements in the multireplace (starting at 2).

Note that:

  • if x < 0.1 the result will be zero! This will break the code. So x should be at least 0.1;
  • if x > 100, that is, 100.1, the return will be (n + 1), which will also break the code;
  • all options get the same “bandwidth”, meaning that there is no ration like in @StorybookParagon’s solution. If you have 3 options inside the multireplace, the points of inflection will be at every (100 / 3), that is, at every 33.33…. In other words, from 0 to 33.33… multireplace will return the first option, from 33.4 to 66.66…, the second and from 66.7 to 100, the third.

This should work no matter how many elements you have inside the multireplace.

Edit:

  • if x < 0.1 the result will be zero! This will break the code. So x should be at least 0.1;
  • if x > 100, that is, 100.1, the return will be (n + 1), which will also break the code;

As I wrote this I realized that you can circunvent this problem if you use this:

@{ (round( ((x * n) / 100) + 0.5 )) One | Two | … | Umpteen | Umpteen }

Note that the last option in the multireplace must be duplicated!

Try writing a program in Java! :joy:

3 Likes

It’s a loving homage to how complicated attempting to cram everything to take up the least space. :joy:

Thanks for showing how you would do it.

1 Like