Here's a solution that lets you use your ranges. Note: I tried to use List::Util::first instead of grep, but there appears to be a bug with the 'first()' function.
use strict;
sub rank {
local $_ = shift;
tr/OBAFGKM/1-7/;
$_;
}
# Enter questions and scores and answers
# Enter answers as a range of values.
my %scores = (
1 => { 1 => { O9=>'B1',
K1=>'K2',
},
0.5 => { B4=>'B5',
B7=>'B7',
},
},
2 => { 1 => { K8=>'K9',
},
0.5 => { F1=>'F2',
},
},
);
my %ranges;
for my $question (keys %scores) {
my $scores = $scores{$question};
my @ranges;
for my $score (keys %$scores) {
my $ranges = $scores->{$score};
push @ranges, map { [ $score, rank($_), rank($ranges->{$_}) ]
} keys %$ranges;
}
@ranges = sort {$a->[1] cmp $b->[1]} @ranges;
$ranges{$question} = \@ranges;
}
my %answers = (1 => 'K2', 2=>'F1');
my $total = 0;
while (my ($qu, $ans) = each %answers) {
$ans = rank($ans);
my ($aref) = grep { $_->[1] le $ans and $ans le $_->[2]
} @{$ranges{$qu}};
$total += $aref->[0] if defined $aref;
}
print $total,"\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.