mIRC Home    About    Download    Register    News    Help

Print Thread
sorting a text file. #209328 10/02/09 04:27 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
Code:

alias twisteron .enable #twister | echo $active Twister script is now ON.
alias twisteroff .disable #twister | echo $active Twister script is now OFF.


#twister on
alias -l twistnum {
  inc -e %twister.num [ $+ [ $chan ] $+ . $+ [ $network ] ]
  .timer 1 %twister.num [ $+ [ $chan ] $+ . $+ [ $network ] ] $$1-
}

on *:text:*!twister:#: {
  if (*!twister !iswm $$1) halt
  inc -e %twister.running [ $+ [ $chan ] $+ . $+ [ $network ] ]
  if (%twister.running [ $+ [ $chan ] $+ . $+ [ $network ] ] == 1) {
    unset %twister.num [ $+ [ $chan ] $+ . $+ [ $network ] ]
    msg $chan $nick Has Started Playing Twister:
    unset %moves
    unset %push
    %push = nobody
    %moves = 0
    var %fall $rand(1,4)
    while (%fall < 4) {
      inc %moves
      %push = $nick
      twistnum describe $chan Spins The Twister Board $iif(%twister.num [ $+ [ $chan ] $+ . $+ [ $network ] ],Again)
      var %colour $r(1,15)
      twistnum msg $chan $nick must put their $gettok(Left Hand.Right Hand.Left Foot.Right Foot,$r(1,4),46) on $+($chr(3),%colour,$chr(44),%colour,--)
      var %fall $rand(1,4)
    }
    var %noob = $nick(#,$r(1,$nick(#,0)))
    if (%fall > 3) twistnum msg $chan Sorry, $nick $+ ! You fell on %noob .  4,1 $+ $read(random.txt) 8,1 $+ You almost had %moves successful attempts!!!
    /write moves.txt $nick $+ : %moves
    .timer 1 $calc(%twister.num [ $+ [ $chan ] $+ . $+ [ $network ] ] + 1) unset %twister.running [ $+ [ $chan ] $+ . $+ [ $network ] ]
  }
  if (%twister.running [ $+ [ $chan ] $+ . $+ [ $network ] ] > 1) msg $chan Please wait your turn, $nick $+ .
}

#twister end

on *:TEXT:*:#: {
  if ($strip($1) == !push) {
    /msg $chan 1,13 $nick Just pushed %push off the mat!!!
    /msg $chan $nick CHEATED!!!

  }
  if ($strip($1) == !add) { /write random.txt $2- | /msg $chan 1,13 $nick Just Added $2- To The Random Twister Comments List }
  if ($strip($1) == !twisthelp) { notice $nick Twister commands are: !ten, !played, !twister, !push, !add, !best, !list, !twisthelp }
  if ($strip($1) == !best) { 
    var %file = moves.txt
    /filter -ffcuet 2 32 %file %file
    /msg $chan 4,1 $+ the person that had the most moves on the Twister mat is $read(moves.txt, 1 )
  }
  if ($strip($1) == !list) { play $nick random.txt }
  if ($strip($1) == !played) { msg $chan Twister has been played very badly $lines(moves.txt) times. }
  on *:TEXT:*:#: {
    if ($strip($1) == !ten) {
      msg $chan The ten best moves so far are:
      var %i = 0
      while (%i <= 10) {
        if ($read(moves.txt,%i)) {
          msg $chan $v1
        }
        inc %i
      }
    }
  } 


this is the whole thing.

What i am using to get the top ten is:
Code:
 on *:TEXT:*:#: {
    if ($strip($1) == !ten) {
      msg $chan The ten best moves so far are:
      var %i = 0
      while (%i <= 10) {
        if ($read(moves.txt,%i)) {
          msg $chan $v1
        }
        inc %i
      }
    }



How would i go about sorting the text file so that duplicates are not displayed?

here is a little piece of the textfile(moves.txt)

Code:
ShadowMaster: 14
ShadowMaster: 12
Corbie: 10
aceroms: 10
ShadowMaster: 10
Giggles: 9
Giggles: 9
wolluk: 8
Corbie: 8
Corbie: 8
Corbie: 8
ShadowMaster: 8
ShadowMaster: 8
ShadowMaster: 8
Giggles: 7
Corbie: 7


Re: sorting a text file. [Re: kkoser] #209330 10/02/09 05:45 PM
Joined: Jul 2006
Posts: 3,741
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,741
Instead of trying to not display the same line, you should do something to not have duplicate lines.When writting to the file, check if the nick already has a score, if so, replace the line by the new score.

Last edited by Wims; 10/02/09 05:45 PM.

Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: sorting a text file. [Re: Wims] #209331 10/02/09 06:09 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
That is absolutely a great idea. How would i start? i will go over the help file again to see what i can learn. a little push in the right direction would help. smile thanks.

Re: sorting a text file. [Re: kkoser] #209333 10/02/09 06:38 PM
Joined: Jul 2006
Posts: 3,741
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,741
Use $read with the s switch, it will scan the file for you and try to match the first word and will return the rest of the line.

if you have a file like :

Wims 2
kkoser 1
test

$read(file,s,Wims) return 2
$read(file,s,kkoser) return 1
$read(file,s,test) return $null even if he found something, because he return the rest of the line, but $readn will have a value.


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: sorting a text file. [Re: Wims] #209334 10/02/09 06:44 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
ok thanks. let me work on that for a while.

Re: sorting a text file. [Re: Wims] #209335 10/02/09 07:03 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
ok how about something like this?

Code:

 if $read(moves.txt,s,$nick) != $nick $+ : %moves {
      /write moves.txt $nick $+ : %moves
    }


not sure about it..doesnt seem to work right now.

Re: sorting a text file. [Re: kkoser] #209337 10/02/09 07:20 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Code:
if ($read(moves.txt,s,$nick)) { var %rn = $+(-l,$readn), %sol = $calc($gettok($ifmatch,2,58) + %moves) }
write %rn moves.txt $nick $+ : $iif(%sol,%sol,%moves)



I don't use text files, ini would probably server you better here, but this should do what you want.

Quote:

This will check if the nick exists in your text file.

if($read(moves.txt,s,$nick))

If it does it sets two local variables.

var %rn = $+(-l,$readn)
This saves the line number so you know which line to overwrite. -l#

This adds their new score to their old score.
%sol = $calc($gettok($ifmatch,2,58) + %moves)

Then, regardless if the nick matched or not, it write their score to the file.

write %rn moves.txt $nick $+ : $iif(%sol,%sol,%moves)

This writes to the specified line %rn has saved if it exists. If %sol exists it writes this solution instead of the recently gained score.

$iif(comparison is $true,Do This,Else Do This)



Hope this helps. smile

Re: sorting a text file. [Re: DJ_Sol] #209339 10/02/09 07:32 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
thanks for explaining it. i really appericiate that. let me work with it for a bit.

if i change the text file to an .ini, you said it would be easier to do?

Re: sorting a text file. [Re: kkoser] #209343 10/02/09 08:57 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
If you change it to an ini file you have to handle it differently.

You have to use /writeini and $readini. The difference is it stores data in an organized fashion.

Where a text file just stores all info as it is placed in it, an ini file is organized into sections.

File.ini

[Section]
item1=data
item2=data
item3=data

So item would be the nick or address of the participant and 'data' would be their score.

So let's make an ini file called Games.ini, in it you will have a section for Twister. In this section you will store the score of each $nick.

/writeini games.ini twister $nick %moves

To check on a users score you use $readini.

$readini(games.ini,twister,$nick) == SCORE

So this would replace the code I wrote earlier.
Code:
if ($readini(games.ini,twister,$nick)) { var %score = $calc($ifmatch + %moves) }
writeini games.ini twister $nick $iif(%score,%score,%moves)


$ifmatch returns the result of the last comparison.

if ($readini(games.ini,twister,$nick)) <here $ifmatch would return either the score if $true, or $false if there was no match.>

So since $ifmatch == their score, I can add $ifmatch to the score they just recieved to come up with their total score.

Another way I could write this is.

Code:
var %score = $calc(%moves + $readini(games.ini,twister,$nick))
writeini games.ini twister $nick %score


This will add their new points (%moves) to the value $readini(games.ini,twister,$nick) returns. If it returns $null, or 0, the new score stays the same, otherwise it is added to the old score.

Re: sorting a text file. [Re: DJ_Sol] #209344 10/02/09 09:34 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
wow what a difference!

it works just as you wrote it.

i don't want to add the scores to gether but to replace it if it is higher than the previous one.

so, i should use $replace? or just change it when it is larger?

Code:

var %score = %moves 

if ($readini(games.ini,twister,$nick)) < %moves
   writeini games.ini twister $nick $iif(%score,%score,%moves)


Re: sorting a text file. [Re: kkoser] #209357 11/02/09 12:39 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
then you just write it.

/writeini games.txt twister $nick %moves

This will overwrite the existing entry.


Code:
if ($readini(games.ini,twister,$nick) < %moves) { writeini games.txt twister $nick %moves }

Re: sorting a text file. [Re: DJ_Sol] #209399 11/02/09 11:56 PM
Joined: Apr 2008
Posts: 51
K
kkoser Offline OP
Babel fish
OP Offline
Babel fish
K
Joined: Apr 2008
Posts: 51
ok thank you for the help. this is the code i used to get it going.

Code:

    if ($readini(games.ini,twister,$nick) = $null)  { writeini games.ini twister $nick %moves } 
    if ($readini(games.ini,twister,$nick) < %moves) { writeini games.ini twister $nick %moves }


Re: sorting a text file. [Re: kkoser] #209400 12/02/09 12:05 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
both those lines do the same thing.

Get rid of the first one. smile

if ($readini(games.ini,twister,$nick) == $null then it is less than %moves.

Know what I'm saying?