in reply to Efficient Rating of Test Answers

Ughh! No matter how you slice it, that's an icky problem. I try to avoid those nasty if-elsif-else statements with a hash table. japhy calls it a 'Dispatch Table', which sounds like a great name to me. The idea is that the keys in the hash point to subroutines:
my %dispatch = ( g6 => \&add_one, g7 => \&add_one, g8 => \&add_one, g9 => \&add_one, g0 => \&add_half, g1 => \&add_half, g2 => \&add_half, g3 => \&add_half, g4 => \&add_half, g5 => \&add_half, k0 => \&add_half, k1 => \&add_half, k2 => \&add_half, k3 => \&add_half, k4 => \&add_half, k5 => \&add_half, ); sub add_one { my $total = shift; $total += 1; return $total; } sub add_half { my $total = shift; $total += 1; return $total; }
And you access the table like so:
my $total = 10; $total = $dispatch{'g6'}->($total); $total = $dispatch{'k5'}->($total); print $total, "\n";
This is not a complete solution for you, but it is a start.

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

Replies are listed 'Best First'.
Re: (jeffa) Re: Efficient Rating of Test Answers
by Hofmator (Curate) on Aug 14, 2001 at 13:37 UTC

    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

      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'
      ----------------------------------------------------