mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Nov 2007
Posts: 9
F
faine Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
F
Joined: Nov 2007
Posts: 9
Hello!

I need a little help. I'm trying to add an extra function to a quiz script.

The script is reading categories, questions and answers from a text file.
The question file consists of alternating lines of categories, questions and answers with no blank lines at the end.

What I would like to do is to make the script to count and list how many there are of each category in the question file.

I would like to make it look something like this:

Categories: Geography (2), History (1), Science (5), ...

Any help greatly appreciated.

Thanks.

Joined: May 2007
Posts: 89
T
Babel fish
Offline
Babel fish
T
Joined: May 2007
Posts: 89
Hoy

It's not very difficult. But you must first give us the format of the actual text file containing the data from where the filtering is to be done. That will greatly help.

Regards smile


tropnul
Joined: Nov 2007
Posts: 9
F
faine Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
F
Joined: Nov 2007
Posts: 9
Thanks for the reply.

In the text file categories, questions and answers are in separate lines:

Category
Question
Answer
Category
Question
Answer

if that was what you were asking for smile

Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
The only thing I can think of right now is to $read every 3rd line and inc either a variable or a hash table item with the same name as the line being read in (the category) then displaying it. But that seems a bit awkward and I'm sure there's a better way to go about it.


Invision Support
#Invision on irc.irchighway.net
Joined: Jun 2006
Posts: 508
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Jun 2006
Posts: 508
try this out...
Code:
alias catscan {
  if !$file($1-) { echo -aict info * /catscan: Unable to open $+(',$1-.') | return }
  .fopen catscan $qt($1-)
  if $ferr {
    .fclose catscan
    echo -aict info $!fopen error
    return
  }
  while !$feof {
    if $fread(catscan) {
      inc -u $+(%,cats.,$v1)
      noop $fread(catscan) $fread(catscan)
    }
  }
  .fclose catscan
  var %i = 1,%a
  while $var($+(%,cats.,*),%i) {
    %a = %a $gettok($v1,2,46) - $($v1,2) :
    inc %i
  }
  echo -a Categories: %a
  unset %cats.*
}

Usage: /catscan FILENAME

Joined: Nov 2007
Posts: 9
F
faine Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
F
Joined: Nov 2007
Posts: 9
Thank you deegee, works great smile

but I still have problem with integrating it in the quiz script.

The question file loaded in the quiz script is defined as %qs_file. It would be great if your code could identify which file is loaded without the need of giving the filename again to catscan.
Is that possible?

I could't figure out how to change this in your code frown

Joined: Jun 2006
Posts: 508
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Jun 2006
Posts: 508
This has an extra line (the first line) that allows you to call it with or without a filename. On its own, /catscan will use the %qs_file value, or you can use /catscan filename to specify another file.
Code:
alias catscan {
  if !$0 { tokenize 32 %qs_file }
  if !$file($1-) { echo -aict info * /catscan: Unable to open $+(',$1-.') | return }
  .fopen catscan $qt($1-)
  if $ferr {
    .fclose catscan
    echo -aict info $!fopen error
    return
  }
  while !$feof {
    if $fread(catscan) {
      inc -u $+(%,cats.,$v1)
      noop $fread(catscan) $fread(catscan)
    }
  }
  .fclose catscan
  var %i = 1,%a
  while $var($+(%,cats.,*),%i) {
    %a = %a $gettok($v1,2,46) - $($v1,2) :
    inc %i
  }
  echo -a Categories: %a
  unset %cats.*
}

Joined: Nov 2007
Posts: 9
F
faine Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
F
Joined: Nov 2007
Posts: 9
It works great.
Thank you again smile


Link Copied to Clipboard