mIRC Home    About    Download    Register    News    Help

Print Thread
#128105 21/08/05 12:34 AM
Joined: Aug 2004
Posts: 26
M
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Aug 2004
Posts: 26
I've been trying to make a script that, when someone says a word (Say, "pickle"), it will trigger (on 1:TEXT:*pickle*:#:), and then find the word in the post, along with a second word. I'm trying to make it so that this script essentially removes any words between two words. Basically, if you say the sentence, "I have a pickle, a cucumber, two carrots and a handful of greenbeans," it will pick up on Pickle, then figure out what number word it is in the post, and remove everything between "pickle" and "carrots and a," leaving, "I have a handful of greenbeans." Obviously not the actual text, but that's the idea.
Originally, I had a script:
Code:
on 1:TEXT:*Pickle*:#: { set %textlength 1 | set %Wordnum 1 | .timertext 0 0 /herewego $1- }
alias HereWeGo {
if ($eval(%Wordnum,2) != Pickle) { inc %Wordnum }
else { msg $active The words after Pickle were: $eval($ $+ $calc(%wordnum + 1) $+ -,2)

Which got the data for the text after Pickle. Using the same thing but replacing "pickle" with "carrots and a" and:
Code:
else { msg $active The words before carrots and a were: $eval($ $+ 1- $+ $calc(%wordnum + 1),2)   

Made it so I could get the words before carrots and a, as well.
The problem was that I couldn't get both of the two in the same line, much less get them to combine the data.
So, is there an easier way to find out what number a word is? Some simple identifer, or something?

Note: I have an extra variable set in this. I had to rewrite it from memory when I posted it here, so I've forgotten the exact wording, but it used both variables and worked properly.

Last edited by MercuryD; 21/08/05 12:35 AM.
#128106 21/08/05 12:13 PM
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Code:
on *:TEXT:*pickle*:#:{
  if ($regex($1-,/^(.*?)pickle(.*?)carrots and a(.*)$/) {
    echo $target PICKLE FOUND: before: $regml(1) $+ ; between: $regml(2) $+ ; after: $regml(3) $+ .
    echo $target NEW LINE: $regml(1) $+ $regml(3)
  }
  ; second word not fou!nd, doing nothing
}


If the words have specials characters, you need to escape them with \ in the regex or they will have another meaning.

#128107 21/08/05 05:33 PM
Joined: Aug 2004
Posts: 26
M
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Aug 2004
Posts: 26
I'm trying to remove OOC brackets (()), so I would assume that those count as special characters. Where do I put the \ in the regex? I've never used it before.

#128108 21/08/05 09:28 PM
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Ah, if you just want to remove everything between brackets, you can use
Suppose %input contains the input line, probably $1- or whatever, then this line will set %res to the line with all "(anything)" removed.

var %res, %q = $regsub(%inputtext,/\([^()]*+(?R)*[^()]*+\)/g,,%res)

var %input = abc(defdef)ghi(j(((k))l))mno
=> %res will be abcghimno


I'm not sure what OOC means, so if there's some situations you don't want removed, just tell...

ps: best would be to read a regex tutorial, preferrably one explaining regex in mIRC. Also download http://pcre.org/pcre.txt for the documentation of the library used in mIRC (read from line 1574, before is not useful for mIRC scripters)

#128109 22/08/05 11:34 PM
Joined: Aug 2004
Posts: 26
M
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Aug 2004
Posts: 26
I think I understand, though I have a few questions.
1. Where does %q come from?
2. What purpose does %Input have? We var %res to information, then /var %Input to $1-, but they don't affect each other.
3. What should %Inputtext be set to?
4. What context is this line being used in?

Last edited by MercuryD; 22/08/05 11:40 PM.
#128110 23/08/05 07:27 AM
Joined: Aug 2004
Posts: 26
M
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Aug 2004
Posts: 26
Apparently I can't edit that post any more. I changed %Inputtext to %input, and it worked. Thanks again. I'll read up on the reg stuff.

#128111 23/08/05 12:30 PM
Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
Ok here's some explanation:
1) %q is not used, but since $regsub returns a number you have to do something with it:
$regsub(..) alone on a line will give * 1 no such command
The standard way is !.echo -q $regsub(...) to do nothing with the output. Other ways are
$null($regsub(...))
if ($regsub(...)) { }
var %nothing = $regsub(...)


2 and 3) %input and %inputText should have been the same var blush But actually, you can replace it with $1- or $? or whatever input text you want to have changed. The /var %inputtext is easier to make an example, in your script you should most likely just use $1-. The first parameter of $regsub is the input text, the second is the regex, 3rd is replacement text, 4th is %variable that should contains the result. From then on you use %res instead of $1- for the changed text.

4) You can just include this (adapted) line in whatever place you need it, alias definitions, event handlers, dialog handlers, ...

A basic example:
Code:
on *:TEXT:*:#:{
  var %res
  if ($regsub($1-,/\([^()]*+(?R)*[^()]*+\)/g,,%res)) {
    ; At least one replacement is made -> echo changed line and halt default line
    echo -mbflrt $target $+(<,$nick,>) %res
    haltdef
  }
}


Link Copied to Clipboard