mIRC Home    About    Download    Register    News    Help

Print Thread
#48579 10/09/03 11:23 PM
S
sunsuron
sunsuron
S
I am making a trivia script. The hard part is the sorting and listing from a text file which contain user scores/point. If I have a file called score.txt which contain all the user's point from /write command like this:
Code:
99
35
67
1
33
11
33
33
144
3

How is it possible I can sort and list all this random numbers? What I did so far is getting the max value confused ,
Code:
 
 :max
  var %_min 1
  var %_max $lines(quiz\score.txt)
  var %_max_val 1
  while (%_min <= %_max) {
    set %_comparison $read(quiz\score.txt, t, %_min)
    if (%_comparison > %_max_val) var %_max_val %_comparison 
    inc %_min
  }
  return %_max_val
  halt


Last edited by sunsuron; 10/09/03 11:31 PM.
#48580 10/09/03 11:25 PM
C
codemastr
codemastr
C
/help /filter

#48581 10/09/03 11:50 PM
S
sunsuron
sunsuron
S
aahh.. the help files blush grin

#48582 11/09/03 02:21 AM
S
sunsuron
sunsuron
S
Thanks. /filter really does a good job. For the previous alias, I thought I can produce something recursive with the max and min values and also the middle of it.

#48583 11/09/03 04:05 AM
Joined: Mar 2003
Posts: 1,256
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Mar 2003
Posts: 1,256
Actually, trivia scripts can grow huge user-databases -- using a hash tabl;e to store that stuff would be infinately superior to a txt file.

#48584 11/09/03 04:08 AM
S
sunsuron
sunsuron
S
I've tried but it disappear after quitting mirc. even after I've already /hsave it.

#48585 11/09/03 04:09 AM
Joined: Dec 2002
Posts: 3,015
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 3,015
You have to /hload it on startup remember.

#48586 11/09/03 06:35 AM
S
sunsuron
sunsuron
S
yep... always missing the basic.. my bad :tongue:

#48587 11/09/03 08:30 AM
S
sunsuron
sunsuron
S
..mmmm.. any chance I can sort item in hash tables?

#48588 11/09/03 10:28 AM
Joined: Jan 2003
Posts: 2,125
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,125
Hash tables aren't meant to be sorted, it's a structure that offers fast retrieval of items but no form of ordering/sorting. However, you can /hsave the table to a temp file (using the -i switch, to save it in an .ini format) and sort the file itself with /filter. This is fast enough, far faster than looping through the hash table items. For example, if you want to get the highest/lowest number in the table called "blah" you can use something like this:
Code:
  .remove temp.htb
  hsave -i blah temp.htb
  filter -ffcuter 2-999999 2 61 temp.htb temp.htb
  var %high = $read(temp.htb,n,1), %low = $read(temp.htb,n,$filtered)
  echo -a Item $gettok(%high,1,61) has the highest number: $gettok(%high,2,61)
  echo -a Item $gettok(%low,1,61) has the lowest number: $gettok(%low,2,61)
This is a descending (-e), numeric (-u) sorting of the 2nd column, using the "=" character (ascii number 61) as the column separator (-t 2 61). After running this code, type
/run notepad temp.htb
and take a look at the contents of the file to better understand what's happened.

#48589 12/09/03 06:22 PM
S
sunsuron
sunsuron
S
You are right! Thanks for the tips.


Link Copied to Clipboard