mIRC Homepage
Posted By: chump search and del script - 06/04/07 02:09 PM
hi i am after a lil script that when sum1 types for example
!addnewquote sweet monkey
it adds to a txt file i have called quotes.txt but if it already exists it msg chan it already exists

also the abilty to del a quote such as
!delquote sweet monkey and again it msg chan quote deleted

any help would be much appreciated..thx guyz
Posted By: Spitfire3292 Re: search and del script - 06/04/07 04:45 PM
Just the simple use of /write and $read

Code:
on *:TEXT:!addnewquote *:#:{
if ($read(quotes.txt,w,$2-)) { msg $chan This quote already exists!
halt
}
else {
/write quotes.txt $2-
msg $chan The Quote has been successfully added.
}
}

on *:TEXT:!delquote *:#:{
if (!$read(quotes.txt,w,$2-)) { msg $chan This quote doesn't exist!
halt
}
else {
/write -ds $+ $2-
msg $chan The Quote has been successfully deleted.
}
} 
Posted By: Riamus2 Re: search and del script - 06/04/07 05:40 PM
Don't HALT a script if you don't need to. Also, don't use HALT unless you're trying to halt everything that happens... use RETURN instead.

Also, you forgot the filename in the delete section and $readn works fine to avoid another search of the file.

Code:
on *:TEXT:!addnewquote *:#:{
  if ($read(quotes.txt,w,$2-)) {
    msg $chan This quote already exists!
  }
  else {
    write quotes.txt $2-
    msg $chan The Quote has been successfully added.
  }
}

on *:TEXT:!delquote *:#:{
  if (!$read(quotes.txt,w,$2-)) {
    msg $chan This quote doesn't exist!
  }
  else {
    write -dl $readn quotes.txt
    msg $chan The Quote has been successfully deleted.
  }
} 
Posted By: chump Re: search and del script - 06/04/07 10:27 PM
thx guys just what i was looking 4 smile

just out off interest how would you do it using ini file insteas of txt file?
Posted By: Riamus2 Re: search and del script - 06/04/07 11:13 PM
You wouldn't want to use an INI file for quotes. That's not a good format for them.
Posted By: Kardafol Re: search and del script - 06/04/07 11:14 PM
You could do it with a hash table, however that would mean that mIRC would use loads of memory if there are a lot of quotes.
Posted By: Riamus2 Re: search and del script - 06/04/07 11:17 PM
And there wouldn't be a performance improvement, so there's no need. Hash tables should only be used for data that is being used frequently. Quotes aren't generally used very often.
Posted By: Kardafol Re: search and del script - 06/04/07 11:23 PM
There should be a slight performance improvement, however you probably won't notice it (memory access time VS HDD seek + read). wink
Posted By: Riamus2 Re: search and del script - 07/04/07 02:52 AM
And at the same time, an increase in unnecessary memory usage. In the end, your system's performance will drop (insignificantly, of course, but it's still a drop due to increased memory usage) for the entire time the script is running just to gain a once-in-a-great-while insignificant performance increase in the script.

It's miniscule from the standpoint of the system and the script. What's a few milliseconds difference in a script that's called once every few hours or even days on average? There's not point wasting memory over something like that.

Invision is like that... it has a lot of hash tables that are nothing but settings that are checked rarely. It causes Invision to take 3-5% CPU resources instead of mIRC's typical 0%. That's something I need to work on... replacing many of the hash tables with normal settings file reads/writes.

Hash tables should be used when they can improve performance on items that run regularly. For things that run very rarely, hash tables are a waste of resources unless you're going to unload them after use. And I'm sure that loading the table, checking it, then unloading it will take longer than just reading a settings or quote file once every few hours or few days.
Posted By: sdgsgsdgsgsd Re: search and del script - 08/04/07 02:01 PM
on *:text:!*quote*:#:{ quotehandle $1- }
on *:input:*:{ quotehandle $1- }
alias quotehandle {
if $1 == !quote {
var %n = $iif($2 isnum,$2,$rand(1,$lines(quotes.txt)))
msg $chan %n $+ . $read(quotes.txt,%n)
}
if $1 == !addquote && $2 { write quotes.txt $2- | msg $chan Quote added! }
if $1 == !delquote && $2 isnum { write -dl $+ $2 quotes.txt | msg $chan Quote $2 deleted! }
if $1 == !quotes { msg $chan $calc($lines(quotes.txt)) quotes in database! }
if $1 == !quotesearch { msg $chan $iif($read(quotes.txt, w, * $+ $2- $+ *),$ifmatch,No matches) }
}
Posted By: Riamus2 Re: search and del script - 09/04/07 04:30 PM
A few things...

First, use ELSEIF instead of repeated IF's for something like that. Second, you don't need $calc when getting $lines. Third, you don't need $+ after -dl and before $2. Just -dl $2 is fine.

And I'd suggest some form of error checking. If someone types the command incorrectly, the script won't do anything and he/she will not know why it's not working without some form of error message.
© mIRC Discussion Forums