mIRC Home    About    Download    Register    News    Help

Print Thread
#174357 06/04/07 02:09 PM
Joined: Sep 2006
Posts: 35
C
chump Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Sep 2006
Posts: 35
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

Joined: Mar 2007
Posts: 60
S
Babel fish
Offline
Babel fish
S
Joined: Mar 2007
Posts: 60
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.
}
} 

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
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.
  }
} 

Joined: Sep 2006
Posts: 35
C
chump Offline OP
Ameglian cow
OP Offline
Ameglian cow
C
Joined: Sep 2006
Posts: 35
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?

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
You wouldn't want to use an INI file for quotes. That's not a good format for them.

Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
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.

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
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.

Joined: Jan 2007
Posts: 259
K
Fjord artisan
Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
There should be a slight performance improvement, however you probably won't notice it (memory access time VS HDD seek + read). wink

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
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.

Joined: Apr 2007
Posts: 8
S
Nutrimatic drinks dispenser
Offline
Nutrimatic drinks dispenser
S
Joined: Apr 2007
Posts: 8
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) }
}

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
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.


Link Copied to Clipboard