Add "Max" and "Min" functions to scripting

Features wanted...
Post Reply
Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Add "Max" and "Min" functions to scripting

Post by Papoulka »

Such as

Code: Select all

$biggest = max($setting, $current);
When needed this is much cleaner than an IF-ELSE with string comparisons.

highend
Posts: 13260
Joined: 06 Feb 2011 00:33

Re: Add "Max" and "Min" functions to scripting

Post by highend »

We have user defined functions^^

Code: Select all

    text comp(2, 3);

function comp($a, $b, $c="max") {
    if     ($c == "max") { return ($a >= $b) ? $a : $b; }
    elseif ($c == "min") { return ($a <= $b) ? $a : $b; }
}
or as two single functions:

Code: Select all

function max($a, $b) { return ($a >= $b) ? $a : $b; }
function min($a, $b) { return ($a <= $b) ? $a : $b; }
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Add "Max" and "Min" functions to scripting

Post by PeterH »

Here the function looks a bit less elegant. But you only define it once :roll:
You can call it with up to 5 operands.
Easy to expand to more operands - but I don't remember how many are allowed for for a function :oops:

Code: Select all

   text max(-9, 99, , -999); // test 4 operands, with 3rd omitted

function max($a, $b, $c, $d, $e) {
   $ret = -2147483648;   // smallest 32bit: x'80000000'
   if (($a != '') && ($a > $ret)) { $ret = $a; } // use if specified, *and* if bigger
   if (($b != '') && ($b > $ret)) { $ret = $b; }
   if (($c != '') && ($c > $ret)) { $ret = $c; }
   if (($d != '') && ($d > $ret)) { $ret = $d; }
   if (($e != '') && ($e > $ret)) { $ret = $e; }
   Return $ret;
 }
Edit: corrected initialization of $ret :P
Last edited by PeterH on 01 Jul 2015 12:05, edited 1 time in total.
W7(x64) SP1 German
( +WXP SP3 )

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: Add "Max" and "Min" functions to scripting

Post by bdeshi »

@PeterH: a user function can have at most 11 arguments.
ed. also, any particular reason for the region-dependant $ret? Why not just "-2147483648" or hextodec('0x80000000') ?

btw, added both to UDF exchange. :ugeek:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

FluxTorpedoe
Posts: 855
Joined: 05 Oct 2011 13:15

Re: Add "Max" and "Min" functions to scripting

Post by FluxTorpedoe »

Hmmm, time for golf! :ball:

What about a dirty one-liner?

Code: Select all

  text max("-9, 99, , -999"); // test 4 operands, with 3rd omitted

function max($a){foreach($num,$a,","){$ret=($num>$ret)?trim($num):$ret;}return $ret;}
— unlimited number of arguments
— numbers with or without spaces
Only need is to quote the argument.

Here's the unfurled version:

Code: Select all

function max($a) {
  foreach($num, $a, ",") {
    $ret = ($num > $ret)? trim($num) : $ret;
  }
  return $ret;
}

PeterH
Posts: 2776
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Add "Max" and "Min" functions to scripting

Post by PeterH »

SammaySarkar wrote:@PeterH: a user function can have at most 11 arguments.
ed. also, any particular reason for the region-dependant $ret? Why not just "-2147483648" or hextodec('0x80000000') ?

btw, added both to UDF exchange. :ugeek:
Oh - double error :shock:

Don't want hextodec() for each call, no need to always recalculate. So not converting is best.

But being lazy I used calculator to convert hex to dec, and copied the result - including the dots :oops: :oops: :bug: 1
Now: without quotes they are decimal points :shock: so I added quotes, thinking it's OK :bug: 2

For sure you are right:

Code: Select all

   $ret = -2147483648;   // smallest 32bit: x'80000000'
would have been what I wanted!


@FluxTorpedoe
I think your version can be OK for private use - but as it uses unusual parameters (i.e. not multiple parameters, but one parameter with a list of values, so must be called different) I wouldn't offer it as common - at least without a lot of hints for this situation.
And for an often used, but only once coded function I think that some lines more for "common handling" are OK.

By the way: I'm surprised by your "missing initialization" of $ret...
It seems for un-initialized $ret it's a string compare - where '$ret' is smaller than '1' and '-1'. So the result is correct.
(This is: the character '$' is less than '+', '-', and all numeric digits.)
W7(x64) SP1 German
( +WXP SP3 )

FluxTorpedoe
Posts: 855
Joined: 05 Oct 2011 13:15

Re: Add "Max" and "Min" functions to scripting

Post by FluxTorpedoe »

FluxTorpedoe wrote:for an often used, but only once coded function I think that some lines more for "common handling" are OK.
Sure! :)

Well, I'd tried to empirically circumvent the discrete argument detection with a more holistic approach (special dedication to aurumdigitus), but found no valid catch-all... There ought to be a way though, I feel it somewhere!

BTW, I discovered $* works but returns only the first argument!

And yes, the initialization of $ret was "missing" on purpose! 8)

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Add "Max" and "Min" functions to scripting

Post by Papoulka »

I rest my case.

Post Reply