[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Efficiency of String operators vs. arrays



Patience, please, with a beginner, and auto-ignore syntax errors below :-0

I'm working on a general-purpose multiple-choice quiz engine for the
Internet.  This would include multiple answers with more than one correct
answer (e.g. checkboxes rather than radio buttons on an HTML form).  It's a
lot of fun (and work), but as I work on it I was wondering which would be
more efficient...storing the user's answers and the correct answers in
arrays, or strings.

e.g. let's say a question has 5 possible answers called, say A,B,C,D and E,
Answers B and D are correct.  The user submits a form on which she's
checked answers A, D and E answers.  I can take what CGI gives me and get
them into a form like this:

@user_answ=("A","D","E");
@corr_ans=("B","D");

and then compare them by stepping through the array to create something like

@got_right=("D")
@missed=("B")
@badpick=("A","E")

or, I could put them into strings like this

$user_answ=("ADE");
$corr_ans=("BD");

and then use stuff like

for ($i=0;$i<length($user_answ){
     if (index($corr_ans,$user_answ[$i]) == -1) {
          $badpick=($user_answ",".$badpick);
     }
  }

and something similar for the correct answers to get to something like

$got_right=("D")
$missed=("B")
$badpick=("AE")

Either way, I would later use these to display the original answer text and
some feedback to the user.

Arrays seem more Perlish, but "index" feels faster than iterating through
the array of correct answers for each user answer.  Which is more
efficient?