mIRC Homepage
Posted By: endlos Optional parameters in the middle - 24/06/07 07:14 PM
Hello

I can't figure this out.
"1 2 3" will always be numbers
"A" will get its value from %calc.A


!calc 1 2 3 A
Result: (1*x + 2*y + 3*z) * A

!calc 1 2 A
Result: (1*x + 2*y) * A

so typing all "1 2 3" should not be necessary, just "1 2" or "1"
(avoiding !calc 1 0 0 A).

How can I do that, as "A" wouldnt always be $$4 ?

Thanks
Posted By: Riamus2 Re: Optional parameters in the middle - 24/06/07 07:16 PM
A == $0. $0 is the total number of tokens (seperated by spaces, or another character if you use /tokenize).
Posted By: endlos Re: Optional parameters in the middle - 24/06/07 07:49 PM
Thanks

Now I have a big problem. These are the new possibilities:

!calc 1 2 3 A Land Cruiser
Result: ((1*x + 2*y + 3*z) * A) * %calc.points.LandCruiser

!calc 1 B Monte Carlo
Result: ((1*x) * B) * %calc.points.MonteCarlo

The quantity of numbers will vary, after them only A,B,C,D are allowed and finally a name is needed (which can be 1-3 words)

How can I make the last numbers optional with this?

thanks for the help
Posted By: RusselB Re: Optional parameters in the middle - 24/06/07 08:41 PM
What, if any limit is there regarding the number of numerics that can be supplied. I noticed that you're using x, y, & z as non-specific identifiers for a pending calculation. Are these to be returned as letters, or as calculated (ie: use them as variables). Also do you only use x, y & z, or are other letters possbile based upon the number of numerics supplied. If the latter, where do the letters start, and if there are too many numerics supplied to cover the letters allocated, do the letters repeat, or is an error expected?
Posted By: qwerty Re: Optional parameters in the middle - 24/06/07 10:00 PM
Assuming there's always going to be 1 to 3 numbers (no more, no less), you can use regular expressions to match the strings you want and capture the useful substrings to format your reply:
Code:
on *:text:!calc *:#yourchannel:{
  var %a = $regsubex(calc,$2-,/^(\d)( \d|)( \d|)( )([ABCD])((?: \w+){1,3})$/, $&
    (( $+ $remove(\1*x + \2*y + \3*z,\4+ *z,\4+ *y) $+ ) * \5) * $($+(%,calc.points.,$remove(\6,\4)),2))
  if $regml(calc,0) {
    ; result is stored in %a
    msg # %a
  }
}
Posted By: endlos Re: Optional parameters in the middle - 25/06/07 10:51 AM
RusselB

Yes the letters (x,y,x) are %variables. All are numeric values and will show One numeric result.

For example, the value expected for a pilot who won 100 Level1, 200 Level2, 300 Level3 races and so on; with an Skill Level of A (or B,C,D), all of it multiplied by a phrase (1-3 words) which will have its own variable value %calc.ThatPhrase like this:

!calc 100 200 300 0 0 0 0 A Land Cruiser
Result: ((100* %calc.r1 + 200* %calc.r2 + 300* %calc.r3 + 0* %calc.r4 + 0* %calc.r5 + 0* %calc.r6 + 0* %calc.r7 ) * %calc.A) * %calc.multiplier.LandCruiser

so ...
!calc 100 200 300 A Land Cruiser
should show the same result

I will try now the regex suggestion from qwerty, but I'm not familiar with that yet so it will take me a while.

Thanks for the help so far
Posted By: qwerty Re: Optional parameters in the middle - 25/06/07 01:30 PM
Your last post gave a clearer, different picture of what you want to do (it would have helped if you gave all that information in your first post). It does make sense now, although you still give contradictory information with respect to the last variable. Is it %calc.points.<text> or %calc.multiplier.<text> ? The following piece of code assumes the latter; you'll have to modify it yourself if you want otherwise:
Code:
on *:text:!calc *:#yourchannel:{
  if $regex(c1,$2-,/^(\d++(?: \d++)*) ([ABCD])((?: \w++){1,3})$/) {
    var %a = ( $regsubex(c2,$regml(c1,1),/(\d+)/g,\1*%calc.r\n +) ) $&
      $+(*%calc.,$regml(c1,2) *%calc.multiplier.,$remove($regml(c1,3),$chr(32)))
    ; %a contains the final expression
    msg # $calc(%a)
  }
}
Posted By: endlos Re: Optional parameters in the middle - 25/06/07 02:08 PM
WOW!

It works! Thanks smile

Just one more thing, how can I make it to work no matter people type ABCD or abcd ?

!calc 100 200 300 A Land Cruiser
!calc 100 200 300 a Land Cruiser
Posted By: 5618 Re: Optional parameters in the middle - 25/06/07 02:17 PM
Originally Posted By: endlos

Just one more thing, how can I make it to work no matter people type ABCD or abcd ?

Make the end of the regex:

{1,3})$/)

read
{1,3})$/i)
Posted By: qwerty Re: Optional parameters in the middle - 25/06/07 02:19 PM
Just add an "i" (meaning case insensitive) after the second forward slash in $regex, ie:

if $regex(c1,$2-,/^(\d++(?: \d++)*) ([ABCD])((?: \w++){1,3})$/i) {
Excellent!

I really appreciate your help smile

Thanks!
© mIRC Discussion Forums