mIRC Homepage
Posted By: ohaithar Problem with a script - 21/01/11 07:35 AM
on *:TEXT:i like*:#mychannel:{
if (%flood) { return }
set -u5 %flood On
if ((myreallifename isin $1-) || ($me isin $1-)) msg $chan (message1) $nick $+ .
elseif $read(text.txt) isin $1-) msg $chan (message2) $nick $+ .
else msg $chan $1- also laugh }

This is what i have, and it works as i intended... but not always. For example: someone says "i like stuff" it will skip the first if and elseif statement as normal and do the else statement. If someone says "i like ($me)" it will do the if statement as normal. But if someone says "i like (word thats in file text.txt)" sometimes the elseif statement will activate, but the majority of the time it just goes straight to the else statement. I've been messing with this script for several hours trying to figure out why it's not working 100% of the time like i want, and i can't figure it out. Theres no other scripts running so they aren't conflicting.





Posted By: MegaZeroX Re: Problem with a script - 21/01/11 07:51 AM
mm.. I'm looking at the elseif. What is the ELSEIF statement supposed to do? To me, it says you're reading a line from text.txt and then checking that line against $1-.
Posted By: ohaithar Re: Problem with a script - 21/01/11 07:55 AM
right and if that word or phrase is in $1- then it says (message2) but it only works 5% of the time it seems. Im guesstimating when i say 5%.
Posted By: MegaZeroX Re: Problem with a script - 21/01/11 08:15 AM
Your current code will only check one line from the file against $1-... is that what you want?
Posted By: 5618 Re: Problem with a script - 21/01/11 08:21 AM
$read(text.txt) returns a RANDOM line from the text file.
If the text file contains single words, then you want: $read(text.txt,w,$$3) ($3 is the word after "i like" and $$ means $3 must be present)
If you want to match everything after "i like" then you probably want $read(text.txt,w,$+(*,$1-,*)) or $read(text.txt,w,$+(*,$$3-,*)) depending on if you have included "i like" in your txt file or not.

See /help $read for clarification.
Posted By: ohaithar Re: Problem with a script - 21/01/11 08:39 AM
Ok i changed $read(text.txt) to $read(text.txt,w,$$3) and it works without fail but now it wont work if someone says the word in a plural form. For example: someone says "i like dogs" and the word "dog" is in my text.txt file, it doesnt work but if they say "i like dog" it works. How can i fix that?

Edit: basically i need it so that if the text.txt word is anywhere in the "i like" sentence i need the elseif line to display (message2).
Posted By: Horstl Re: Problem with a script - 21/01/11 10:54 AM
For that kind of matching and $read, a loop over all lines of the file is inevitable.
Code:
  [...]
  else {

    ; loop all lines of the file
    var %n = 1
    while ($read(text.txt,n,%n) != $null) {
      ; check for current line matching the third word
      if ($v1 isin $3) {
        ; output and stop further processing in case of success 
        msg $chan message for line $v1 matching word $3
        return
      }
      inc %n
    }

    ; no success
    msg $chan message for no match
  }


Hash tables would allow you to search for both: any <data> matched by <wildtext> (like $read and w), and any <wilddata> matching <text> (what you're after).
Especially if you have a file with a lot of lines, the method outlined below should perform much better:
Code:
  [...]
  else {

    ; create hash table if it does not exist
    if (!$hget(likefile)) { likefile.load }
    ; check for any hash table data wildmatching the third word (returns first match only)
    if ($hfind(likefile,$3,1,W).data) { msg $chan data $hget(likefile,$v1) matches text $3 }
    ; no match
    else { msg $chan message for no match }
  }

  [...]
}

; load file into a hash table
; hash table items will be a numerical index ("line number"), the content of each line the data for that item
alias likefile.load {
  if ($hget(likefile)) { hfree likefile }
  hmake likefile
  hload -n likefile "text.txt" 
}
Note that your text file now has to contain a list of words with wildcards like *dog* or cat*
As you're not checking the file but the hash table, use the /likefile.load command after any modification of the textfile.
Posted By: Riamus2 Re: Problem with a script - 21/01/11 11:14 AM
In addition to what Horstl gave, if all you are having trouble with are plurals, just add the plurals to your file unless that is a problem for whatever reason.

Hash tables are definitely going to be faster, though. And a regex search can also help considerably by ignoring punctuation and such.
Posted By: ohaithar Re: Problem with a script - 22/01/11 11:20 PM
Alright thank you guys, i think i have it working exactly how i want it to but just one last thing. I also want the script to be triggered by the words "i love" along with "i like" how would i incorporate that in the script?
Posted By: Horstl Re: Problem with a script - 23/01/11 12:09 AM
Possibilities include
Code:
; check text parameters without matchtext restriction
on *:text:*:#chan: {
  if (($1-2 == i love) || ($1-2 == i like)) && ($3 != $null) { 
    [...]
  }
}

; limit matchtext and check second text parameter
on *:text:i l* *:#chan: {
  if ($istok(love.like,$2,46)) { 
    [...]
  }
}


; a regular expression for either case as matchtext
on $*:text:/^i (love|like) ./i:#chan: { 
  [...]
}
Posted By: Tomao Re: Problem with a script - 23/01/11 12:37 AM
Code:
/^i l(ove|ike) ./i
I think only one letter l is needed.
Posted By: Horstl Re: Problem with a script - 23/01/11 02:45 AM
*gives Tomao a cookie byte grin
Posted By: Tomao Re: Problem with a script - 24/01/11 03:57 AM
Horstl, thanks for the cookie, and I take a bite byte of your scrumptious cookie. eek
© mIRC Discussion Forums