mIRC Home    About    Download    Register    News    Help

Print Thread
/write and $read #187352 03/10/07 10:39 PM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
i got this code here to log nicknames

Code:
on *:JOIN:#: {
  if (!$read(nicks.txt, sn, $address) { write nicks.txt $address $nick }
  else { write -l $+ $readn nicks.txt $address $read(nicks.txt, sn, $address) $nick }
}


but how would i make it so that if the user already has 15 logs for him, then it will put his new nickname to the beginning

so basically the maximum nicknames it will log for users is 15 keeping the list the most up to date nicks

Re: /write and $read [Re: pouncer] #187353 03/10/07 10:57 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Code:
on *:JOIN:#: {
  if ($read(nicks.txt, sn, $address)) { write -l $+ $readn nicks.txt $address $gettok($v1,-14-,32) $nick }
  else { write nicks.txt $address $nick }
}


I switched the condition checks around to make it more logical to read.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: /write and $read [Re: starbucks_mafia] #187356 03/10/07 11:19 PM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
thanks starbuck i tested it, works very well!

it removes the first nickname from the line and pushes the entry back 1 slot to add the latest nickname to the end!! thanks

Re: /write and $read [Re: pouncer] #187357 03/10/07 11:22 PM
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
no i found a problem, sorry

if the address doesnt exist, it writes the new nickname once

then if the user changed their nickname again, it overwrites that first slot, wherehas it should be

nick1 nick2

but it just shows nick2

Re: /write and $read [Re: pouncer] #187372 04/10/07 02:21 AM
Joined: Feb 2005
Posts: 342
R
Rand Offline
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
In order for this to work the way you want, you'd have to loop the file, for the $address, and delete the 15th nick, then insert the new nick at the top. This is a pretty dirty process.

Code:
alias addnick {
  var %a = $1 , %n = $2 , %i = 1 , %count = 1
  while ($read(nicks.txt,nw,%a *,%i)) {
    var %i = $readn + 1 , %r = $readn
    inc %count
  }
  if (%count > 14) { write $+(-dl,%r) nicks.txt }
  write -il1 nicks.txt %a %n
}


/addnick $address $nick


Of course, you could do it the cleaner way with an ini file:

Code:
alias addnick2 {
  var %a = $1 , %n = $2 , %nicks = $readini(nicks.ini,nicks,%a)
  if (!$istok(%nicks,%n,32)) {
    if ($numtok(%nicks,32) > 14) { var %nicks = $deltok(%nicks,15,32) }
    writeini nicks.ini nicks %a $instok(%nicks,%n,1,32)
  }
  else { writeini nicks.ini nicks %a %n $remtok(%nicks,%n,1,32) }
}


or with hash tables:
Code:
alias addnick2 {
  var %a = $1 , %n = $2 , %nicks = $hget(nicks,%a)
  if (!$istok(%nicks,%n,32)) {
    if ($numtok(%nicks,32) > 14) { var %nicks = $deltok(%nicks,15,32) }
    hadd -m nicks %a $instok(%nicks,%n,1,32)
  }
  else { hadd -m nicks %a %n $remtok(%nicks,%n,1,32) }
}



Hope that helps. smile

Edit: Changed the above last two examples to reflect a better way of handling the nicks. This makes it so that if a nick was towards the bottom, and the person joined with it, it'd move the nick to the first position. smile

Last edited by Rand; 04/10/07 02:32 AM.
Re: /write and $read [Re: pouncer] #187373 04/10/07 02:21 AM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Right. $gettok() isn't as forgiving with negative indices as I thought.

Code:
on *:JOIN:#: {
  if ($read(nicks.txt, sn, $address)) {
    var %oldnicks = $v1, %starttok = $iif($numtok(%oldnicks, 32) >= 14, -14, 1)
    write -l $+ $readn nicks.txt $address $gettok(%oldnicks,$+(%starttok, -), 32) $nick
  }
  else { write nicks.txt $address $nick }
}


If you want it to store each nick only once:

Code:
on *:JOIN:#: {
  if ($read(nicks.txt, sn, $address)) {
    var %nicks = $addtok($v1, $nick, 32), %starttok = $iif($numtok(%nicks, 32) >= 15, -15, 1)
    write -l $+ $readn nicks.txt $address $gettok(%nicks,$+(%starttok, -), 32)
  }
  else { write nicks.txt $address $nick }
}

Last edited by starbucks_mafia; 04/10/07 03:07 AM.

Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: /write and $read [Re: starbucks_mafia] #187374 04/10/07 02:55 AM
Joined: Feb 2005
Posts: 342
R
Rand Offline
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Originally Posted By: starbucks_mafia
If you want it to store each nick only once:

Code:
on *:JOIN:#: {
  if ($read(nicks.txt, sn, $address)) {
    var %nicks = $addtok($v1, $nick, 32), %starttok = $iif($numtok(%nicks, 32) >= 14, -14, 1)
    write -l $+ $readn nicks.txt $address $gettok(%nicks,$+(%starttok, -), 32)
  }
  else { write nicks.txt $address $nick }
}


Not trying to nag you, but, that won't actually work the way you intend it to. If the nick exists, it'll only give you 14 of the nicks (not 15).

Also, lets say that "Bob" is in position 1 ($gettok(%nicks,1,32)), if the person joins as "Bob" and you use $addtok(), it won't add the nick since it's already there, and, it will remove Bob from the list since it's using -14.

Re: /write and $read [Re: Rand] #187375 04/10/07 03:18 AM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
OK, I've edited the original to use 15 instead of 14. If the order is an issue it can easily be resolved with $remtok().

Code:
on *:JOIN:#: {
  if ($read(nicks.txt, sn, $address)) {
    var %nicks = $addtok($remtok($v1, $nick, 1, 32), $nick, 32), %starttok = $iif($numtok(%nicks, 32) >= 15, -15, 1)
    write -l $+ $readn nicks.txt $address $gettok(%nicks,$+(%starttok, -), 32)
  }
  else { write nicks.txt $address $nick }
}


Point being $read()'s search capability is perfect for this particular usage and there's no need to loop anything.

Edited: Did it wrong again. Should use $remtok(), not $instok(), to get right behaviour.

Last edited by starbucks_mafia; 04/10/07 07:47 AM.

Spelling mistakes, grammatical errors, and stupid comments are intentional.
Re: /write and $read [Re: starbucks_mafia] #187378 04/10/07 04:18 AM
Joined: Feb 2005
Posts: 342
R
Rand Offline
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Originally Posted By: starbucks_mafia
OK, I've edited the original to use 15 instead of 14. If the order is an issue it can easily be resolved with $instok().

Point being $read()'s search capability is perfect for this particular usage and there's no need to loop anything.


Right. My first alias was made under the assumption that he wanted them on different lines since I didn't see any useage of $gettok() on his behalf in his original file.

Hence the reason I supplied the second and third alias (which contain no loops). Your new code now does the same thing my last two aliases do. I used $readini() and $hget() for the examples. I figured you had $read() covered well enough to begin with. smile