mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Mar 2003
Posts: 9
D
dak Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Mar 2003
Posts: 9
The script I have been working on, reads a .txt file with about 500 lines in it (quotes). I have the random read working, I also have it so you can specify what line you want it to read. But i cant seem to get the -w switch to work the way i want it to

Code:
 
on *:text:.find quote *:#:{
  set %quote $read(C:\bot\bot_log_pub.txt, w, * $+ $3 $+ *, $calc($readn + 1) )
  /msg # %quote
  /ignore -u5 $nick
}
 


for eg: .find quote dak
would return the first line that has *dak* in it, then the next time you use the .find quote * , it will start from the line it just read + 1. this is fine, untill it gets to the last line in the .txt file.
at that point, i cant seem to find a way to reset $readn back to 1 after it has hit the end of the document.


______________________________

123go
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Easiest way to reset $readn to 0 is to just do something which will cause $read to not find anything. Just add a line at the beginning or end of your code with something like .echo -q $read(resetting readn,0) - mIRC will try to read line 0 of file 'resetting readn' and fail, which will reset $readn to 0.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Mar 2003
Posts: 9
D
dak Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Mar 2003
Posts: 9
actually, i just fixed the problem. But now i have a new problem ;x

I would like the script to say what line is being /msg'ed
eg: .quote
Quote 153: <nick>quote line

basically i just need a variable/switch that will display the line the script reads


______________________________

123go
Joined: Dec 2002
Posts: 271
N
Fjord artisan
Offline
Fjord artisan
N
Joined: Dec 2002
Posts: 271
/msg $chan %gote > Found on line: $readn

Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
As starbucks_mafia said, if you call $read() once more after the last line that matches *$3* has been read, it returns $null and $readn is set to 0. So all you have to do is call $read() once more in the script, right? Not exactly.

The 'cycling' of $readn (increasing until there's no other matching line, then set to 0 etc) is only possible if the users query the bot with the same search string. To understand it, consider this scenario: your file has 3 lines that contain "apple" but none that contains "orange". Then:

1) John types ".find quote apple". $readn is initially 0, so the starting line is (0 + 1) = 1.
2) the script finds the 1st line that matches *apple* (starting the search from the 1st line in the file) and messages it to the channel. $read is set to a number, let's say 11.
3) Jack types ".find quote apple".
4) the script finds the 2nd line that matches *apple*, starting the search from the (11 + 1) = 12th line. This 2nd line that contains apple is, fex, the 16th line in the file. $readn is set to that.
5) Jay types ".find quote orange"
6) the script can't find a line matching *orange*, so it sets $readn to 0.
7) Jim types ".find quote apple"
8) you would expect that the script finds the 3rd line that matches *apple*, because John and Jack made it find the 1st and 2nd line earlier. This won't happen though, because Jay's query ruined $readn with *orange* and set it to 0. So, the script will reply to Jim the (0 + 1) = 1st matching line for *apple*.

So, we need a way to store the "$readn" (ie the last matched line number) for each search string separately. We can do it by storing $3- (the search string) to a hash table. $3- would be the item and $readn would be the data. To cut down on text and avoid issues when $3- contains more than 1 words, we'll use $hash($3-,32) instead of $3-. $hash() produces a unique number for a given string.

Code:
on *:text:.find quote *?:#yourchannel:{
  if [color:green]$read2($3-)[/color] != $null || [color:purple]$read2($3-)[/color] != $null { 
    msg # Quote $+($readn,: &lt;,$nick,&gt;) $ifmatch 
    .ignore -u5 $nick
  }
}
alias read2 {
  var %a = $hget(findquote,$hash($1,32)), %b = $read(C:\bot\bot_log_pub.txt,nw,* $+ $1*,$calc(%a + 1)) 
  hadd -m findquote $hash($1,32) $readn
  if %a || %b != $null { return %b }
  halt
}


This is where $read2() comes in. It is just an alias that returns whatever $read() returns but also stores the $readn for each search string used.

Now that we explained the logic, let's see what this /if statement inside the on TEXT event does. /if statements work in a way that if the first part of a "(condition1) || (condition2)" expression is true, the second part isn't evaluated. So:
1) If the first $read2() finds a matching line, "condition1" is true ($read2() is not $null), so mirc stops and /msg's $ifmatch (the matching 1st part of the condition, which are the contents of the matching line returned by $read2() in this case) to the channel.
2) If the first $read2() doesn't find a match, it could mean that either there is no line matching *$3* in the file or that the $read2() call from an earlier time the script was triggered had read the last matching line. In the first case, the script is /halted by $read2(). In the second case, $readn, and consequently the hash table item for $3 ($hash($3,32)), is set to 0, so the second $read2() above starts from $calc(0 + 1) = 1st line. If this second $read2() finds a match (ie != $null) the script /msg's $ifmatch to the channel, otherwise returns.

The /halt at the end of read2 makes sure that the file isn't searched twice if there is no matching line in the file (ie when $read() is $null AND the starting line is 1). If this condition is met for the first $read2(), /halt will prevent the script from evaluating the second $read2() (or anything else).


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Mar 2003
Posts: 9
D
dak Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Mar 2003
Posts: 9
thanks for the help


______________________________

123go

Link Copied to Clipboard