mIRC Homepage
Posted By: faine need help with a quiz script - 04/11/07 04:29 PM
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.
Posted By: TropNul Re: need help with a quiz script - 04/11/07 07:23 PM
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
Posted By: faine Re: need help with a quiz script - 05/11/07 12:22 AM
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
Posted By: Riamus2 Re: need help with a quiz script - 05/11/07 12:57 AM
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.
Posted By: deegee Re: need help with a quiz script - 05/11/07 02:00 AM
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
Posted By: faine Re: need help with a quiz script - 05/11/07 04:09 PM
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
Posted By: deegee Re: need help with a quiz script - 05/11/07 08:55 PM
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.*
}
Posted By: faine Re: need help with a quiz script - 27/11/07 04:10 PM
It works great.
Thank you again smile
© mIRC Discussion Forums