mIRC Homepage
Posted By: ohaithar help with random number on text - 12/02/11 03:34 AM
ok i'm still learning scripting so i'm sort of making a game where if a number is said anywhere in the text a message will display. I need the script to activate if the number in the text line is 100000 or anything greater. All i need to know is how to get the script to activate once the right number has been said. Everything else i think i can handle on my own, hopefully lol.

Also if the number has a comma in it for example 100,000 will that make a difference and if so how can i make it still work?
Posted By: RusselB Re: help with random number on text - 12/02/11 04:23 AM
There may be more efficient ways of doing this, but here's an option.
Code:
on *:text:*:#:{
  var %a = 1, %b = $0
  while %a <= %b {
    if $remove($gettok($1-,%a,32),$chr(44)) isnum 100000- {
      .msg $chan The number $gettok($1-,%a,32) was said.
    }
    inc %a
  }
}

Note: This will activate for every number that is said if the number is 100000 or higher.
If one person said 100010 100,020
Then the script will activate twice. Once for the 100010 and once for the 100,020.
Posted By: ohaithar Re: help with random number on text - 12/02/11 05:57 AM
ok one last thing, some of the numbers will have a $ sign in front so for example if $100,000 was said how do i get the script to activate on that text aswell?
Posted By: RusselB Re: help with random number on text - 12/02/11 10:30 PM
Change $remove($gettok($1-,%a,32),$chr(44)) to $remove($gettok($1-,%a,32),$chr(44),$chr(36))
Posted By: ohaithar Re: help with random number on text - 13/02/11 12:43 AM
Thanks a bunch! Also if i want the script to respond to something in colored text code, how would i do that without turning on color stripping in mirc options? Or is that the only way?
Posted By: Tomao Re: help with random number on text - 13/02/11 01:06 AM
Code:
on $*:text:/^(\44?\d{3}\54?\d{3,})/S:#:.msg # The number $regml(1) was said.
Use regex without a while loop for this. This also strips out control codes.

Please note this is based upon the example you've shown. There might be a possibility it'll fail if you have numbers like this:
Quote:
$1,264.10
for example. The regex may need to be modified.
Posted By: Riamus2 Re: help with random number on text - 13/02/11 01:11 AM
That's definitely better if you only have one large number in a line. Otherwise, you'll only show the first. I'd still use regex even if you want multiple results... I'd just change it to display all of the $regml()'s.
Posted By: Tomao Re: help with random number on text - 13/02/11 01:15 AM
Riamus2, if you don't mind, could you please show me your apporach with this? ^^ Much obliged.
Posted By: Tomao Re: help with random number on text - 13/02/11 02:01 AM
If you still want to go with RusselB's code and to have the control codes stripped, just enclose
Code:
$1-
like this:
Code:
$strip($1-)
Posted By: Riamus2 Re: help with random number on text - 13/02/11 02:23 AM
My regex is not very good yet and I'm having trouble making yours work within $regex() for testing (for example, I'm not sure what \54 is doing), but basically I'd just make it greedy so it will fill multiple $regml()'s with all results and then loop through until $regml(0) and create the output that way. If there's a better way, I'm not sure.
Posted By: Tomao Re: help with random number on text - 13/02/11 03:19 AM
Riamus2, \54 is an octal character and is equivalent to \x2C and mIRC's $chr(44)

And \44 is the same as \x24 and $chr(36)

It's force of habit I use the octal chars. You can do it as \$ for it to be escaped with a backward slash \ , since the dollar sign in regex has its special meaning. For the comma, though, it's not necessary to make it literal. I hope this clears up your confusion.

P.S. you should be able to find a list of octal characters by googling one.
Posted By: Riamus2 Re: help with random number on text - 13/02/11 03:33 AM
Ok, I see. I thought those should be relating to the $ and comma, but the \44 threw me off since 44 is the $chr for comma and then 54 didn't make sense. I didn't think of octal. laugh
Posted By: ohaithar Re: help with random number on text - 13/02/11 04:06 AM
I changed $remove($gettok($1-,%a,32),$chr(44),$chr(36)) to $remove($gettok($strip($1-),%a,32),$chr(44),$chr(36)) but it still refuses to work if the text is using color codes. frown

Posted By: ohaithar Re: help with random number on text - 13/02/11 04:09 AM
Originally Posted By: Tomao
Code:
on $*:text:/^(\44?\d{3}\54?\d{3,})/S:#:.msg # The number $regml(1) was said.
Use regex without a while loop for this. This also strips out control codes.

Please note this is based upon the example you've shown. There might be a possibility it'll fail if you have numbers like this:
Quote:
$1,264.10
for example. The regex may need to be modified.


I tried this code too and it didn't seem to work, also this type of scripting is kind of over my head.
Posted By: RusselB Re: help with random number on text - 13/02/11 04:48 AM
Here's my code with the various changes that you've requested implemented.
Code:
on *:text:*:#:{
  tokenize 32 $strip($1-)
  var %a = 1, %b = $0
  while %a <= %b {
    if $remove($gettok($1-,%a,32),$chr(44),$chr(36)) isnum 100000- {
      .msg $chan The number $gettok($1-,%a,32) was said.
    }
    inc %a
  }
}

Posted By: Tomao Re: help with random number on text - 13/02/11 05:21 AM
I made a foolish mistake. I initially had the caret sign used, which would mean that the regex has to make a match starting with a number or $, or else it will fail. I have corrected the issue and tested it:
Code:
on $*:text:/(\44?\d{3}\54?\d{3,})/S:#:{
  .msg # The number $regml(1) was said.
}
Please be noted that this is not a foolproof regex but will work to match all the number patterns you've shown us. If you encounter a set of numbers like
Quote:
$1,264,543.10
, both RusselB's and mine will fail to match.
Posted By: ohaithar Re: help with random number on text - 13/02/11 05:31 AM
That's ok Tomao my script won't be dealing with decimals, at least not anytime soon.

Thank you everyone i think that's all i need. smile
Posted By: Tomao Re: help with random number on text - 13/02/11 05:39 AM
You're welcome, ohaithar. RusselB's and mine work pretty much the same in the functionality per se, but without the loop and the process steps using $remove and $gettok...I'd say my regex method is going to be faster. Again, I'm not criticizing RusselB's code. He's done a good job at getting your request fulfilled just as dandy.
Posted By: RusselB Re: help with random number on text - 13/02/11 10:59 AM
Quote:
If you encounter a set of numbers like
Quote:
$1,264,543.10
, both RusselB's and mine will fail to match.


I disagree that my code will not match that number.

While I haven't actually tested it (too tired to do so now), my code should work fine for a number like that using the following method.

Comments added to code to show how I believe that my code will work fine for a number like that.
Code:
on *:text:*:#:{
  ;number entered as $1,264,543.10
  tokenize 32 $strip($1-)
  ;control codes stripped and line re-evaluated with standard spaces being token separators.
  var %a = 1, %b = $0
  while %a <= %b {
    if $remove($gettok($1-,%a,32),$chr(44),$chr(36)) isnum 100000- {
     ;$gettok($1-,%a,32) on the first loop where %a = 1, becomes $1,264,543.10
     ;commas and dollar sign removed using $remove leaving 1264543.10
     ;check if 1264543.10 is equal to or greater than 100000
     ;comparison shows true, thus a message using the original entered number $1,264,543.10 is posted back to the channel.
     .msg $chan The number $gettok($1-,%a,32) was said.
    }
    inc %a
    ;var %a increases by 1, making it 2, which is outside of the loop limit, thus ending the loop.
  }
}

Posted By: Tomao Re: help with random number on text - 13/02/11 09:18 PM
Yes, I apologize. It does work for numbers with decimals. I didn't test your code initially but was eyeballing it. I should have done so. For some odd reason, though, mine works now to trigger upon the decimals too, just as yours. I modified it a little to improve it:
Code:
on $*:text:/(.*?\44?\d{3}\54?\d{3,}.*)/S:#:{
  .msg # The number $regml(1) was said.
}
You're welcome to make a comparison between these two methods and see which one is more efficient.
© mIRC Discussion Forums