in reply to (jeffa) Re: Efficient Rating of Test Answers
in thread Efficient Rating of Test Answers

You are describing the general concept of a 'Dispatch Table'. While very useful at times I think that's a little overkill here. Your subroutines do exactly the same thing - add a number - and only this number differs. So why not just store the number as proposed by VSarkiss. Your code would then simplify to

my %lookup = ( # no longer a dispatch g6 => 1, g7 => 1, g8 => 1, g9 => 1, g0 => 0.5, # ... ); my $total = 10; $total += exists($lookup{'g6'})? $lookup{'g6'} : 0; print $total, "\n";
I think I still prefer the regex solution by enoch in this easy case where the keys/answers are so 'handy' to match.

-- Hofmator

Replies are listed 'Best First'.
(jeffa) 3Re: Efficient Rating of Test Answers
by jeffa (Bishop) on Aug 14, 2001 at 19:16 UTC
    Very nice!

    How about generating the table with Perl instead of a text editor? I'll just borrow the regexes you and enoch provided, if you don't mind :D

    my %lookup; foreach my $alpha ('a'..'k') { foreach my $numb (0..9) { $_ = $alpha . $numb; $lookup{$_} = /^g[6-9]$/ ? 1 : /^[gk][0-5]$/ ? .5 : 0; } }
    however, this is not very efficient if your memory is tied behind your back. ;)

    ----------------------------------------------------
     perl -le '$x="jeff";$x++ for(0..4482550);print $x'
    ----------------------------------------------------