mIRC Homepage
Posted By: Ninko Variable problem - 26/04/08 03:34 AM
Hi there,
The following script appears to be working correctly, but variable 'num' doesn't appear to show the correct line the script actually stopped on in the file. For some reason %num is a randum number each time, even though the line it stops at in the file is the same/correct one, its as though the script is too quick and doesn't get chance to update %num each time?

Code:
on 1:TEXT:!test1:#:{
  %var1 = textstring
  %num = 1
  :start
  %var2 = $read("file.txt), %num)
  %var3 = $gettok(%var2,1,58)
  if (%var1 == %var3) {
    goto end
  }
  else {
    %num = %num + 1
    goto start
  }
  :end
  msg $chan %num
}


Any help please?

Thanks


Ninko
Posted By: Wims Re: Variable problem - 26/04/08 03:46 AM
There is a little problem in your code :
Quote:
%var2 = $read("file.txt), %num)
Here, there is a problem with ), and you have only one ", should be $read("file.txt",%num)
But you should use while instead of goto and local variable :
Code:
on *:TEXT:!test1:#:{
  var %s = textstring ,%n = $lines("file.txt")
  while (%n) {
  var %v = $read("file.txt",%n) ,%r = $iif(%v == $gettok(%v,1,58),0,1)
  if (%r) dec %n
  else { msg $chan %n | break } 
 }
}
Posted By: Ninko Re: Variable problem - 26/04/08 03:54 AM
Thanks for the reply. One question though, why are while loops better then goto loops?

Thanks


Ninko
Posted By: argv0 Re: Variable problem - 26/04/08 04:37 AM
they're easier to look at, manage and maintain, and easier to figure out what went wrong when something does.

For instance, upon first glance I had no clue your code even looped until it was rewritten with while

With while, the loop was obvious.
Posted By: qwerty Re: Variable problem - 26/04/08 11:40 AM
Apart from the valid points mentioned in this thread, you don't really need a loop here: you can use $read()'s w switch, to have mirc search the file itself for lines matching textstring:* (type /help $read for more)
Code:
on 1:TEXT:!test1:#:{
  noop $read(file.txt,w,textstring:*)
  msg # $readn
}
© mIRC Discussion Forums