### Re^2: alternatives to if and series of elsif

by gsiems (Chaplain)
 on Jul 03, 2005 at 03:34 UTC ( #471983=note: print w/replies, xml ) Need Help??

in reply to Re: alternatives to if and series of elsif
in thread alternatives to if and series of elsif

Except that neither stick or stick2 return the same results as the original compare subroutine. Consider,

```print "compare: ", compare (10127, 20), "\n";
print "stick: ", stick (10127, 20), "\n";
print "stick2: ", stick2 (10127, 20), "\n";

...

Prints out:

```compare: 15
stick: 20
stick2: 20
```

Replies are listed 'Best First'.
Re^3: alternatives to if and series of elsif
by BrowserUk (Pope) on Jul 03, 2005 at 05:31 UTC

Your right, but I concluded (as I assume other responders did) that it was a bug in the original implementation. One that possibly doesn't show up because the situation doesn't arise in use.

My interpretation of the intent of the original algorithm, is that if a user has acquired a given number of points, and their quota has not yet been incremented to the appropriate level, then it is increased to that level.

My view was that the quota may represent something like "life force" or "energy" or "armour" in a game scenario, which gets increased in stages as the user accumulates points. This is an assumption, but one that seems to fit the facts as presented.

The thing I noticed with the original implementation is that if a user accumlated more points, but didn't accumulate any additional quota (through other mechanisms) between reassessments, then they are penalised (all the way back to the base level), until their points tally took them high enough to be awarded the next level of quota. At which point they regain both the penalised quota, plus the new increase. Vis:

```#! perl -slw
use strict;

sub compare {
my (\$points, \$quota) = @_;

if (\$points > 18000 && \$quota < 24) {
return 24;
}
elsif (\$points > 16000 && \$quota < 23) {
return 23;
}
elsif (\$points > 14000 && \$quota < 22) {
return 22;
}
elsif (\$points > 12000 && \$quota < 21) {
return 21;
}
elsif (\$points > 10000 && \$quota < 20) {
return 20;
}
elsif (\$points > 8000 && \$quota < 19) {
return 19;
}
elsif (\$points > 6000 && \$quota < 18) {
return 18;
}
elsif (\$points > 4000 && \$quota < 17) { #19
return 17;
}
elsif (\$points > 2000 && \$quota < 16) { #17
return 16;
}
return 15;
}

my \$userQuota = 0;
for my \$userPoints ( map{ \$_ * 1000 } 0 .. 19 ) {
printf "Before: %5d : %5d", \$userPoints, \$userQuota;
\$userQuota = compare( \$userPoints, \$userQuota );
printf " After: %5d : %5d\n", \$userPoints, \$userQuota;
}

__END__
P:\test>471983
Before:     0 :     0 After:     0 :    15
Before:  1000 :    15 After:  1000 :    15
Before:  2000 :    15 After:  2000 :    15
Before:  3000 :    15 After:  3000 :    16
Before:  4000 :    16 After:  4000 :    15
Before:  5000 :    15 After:  5000 :    17
Before:  6000 :    17 After:  6000 :    15
Before:  7000 :    15 After:  7000 :    18
Before:  8000 :    18 After:  8000 :    15
Before:  9000 :    15 After:  9000 :    19
Before: 10000 :    19 After: 10000 :    15
Before: 11000 :    15 After: 11000 :    20
Before: 12000 :    20 After: 12000 :    15
Before: 13000 :    15 After: 13000 :    21
Before: 14000 :    21 After: 14000 :    15
Before: 15000 :    15 After: 15000 :    22
Before: 16000 :    22 After: 16000 :    15
Before: 17000 :    15 After: 17000 :    23
Before: 18000 :    23 After: 18000 :    15
Before: 19000 :    15 After: 19000 :    24

This doesn't fit with any pattern I could relate to, so I assumed it was a bug (that doesn't show up in use), with the original implementation.

However, you are correct that my implementations don't correctly comply with even my interpretation of the OPs requirements in as much as I have a fencepost error. The following two replacements correct that deficiency:

```use constant STICK => pack 'C*', 15, map( (\$_) x 2, 15 .. 23 ), (24) x
+ 100;
sub stick{
my \$q = ord substr STICK, 1+ \$_[0] / 1000;
return \$q < \$_[1] ? \$_[1] : \$q;
}

use constant STICK2 => [ 15, map( (\$_) x 2, 15 .. 23 ), (24) x 100 ];
sub stick2{
my \$q = STICK2->[ 1 + \$_[0] / 1000 ];
return \$q < \$_[1] ? \$_[1] : \$q;
}

I guess only kiat will be able to tell us if my assumption was a pragmatic one.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

++ Good call.

Thanks, BrowserUK!

My interpretation of the intent of the original algorithm, is that if a user has acquired a given number of points, and their quota has not yet been incremented to the appropriate level, then it is increased to that level.
That's the right interpretation. When none of the conditions is met, the value returned should be undef or 0, to indicate that nothing should be done (in the code I posted, the returned value when none of the conditions is met is 15).
My view was that the quota may represent something like "life force" or "energy" or "armour" in a game scenario, which gets increased in stages as the user accumulates points. This is an assumption, but one that seems to fit the facts as presented.
Yes, the quota is some kind of energy points. These points are increased in conjunction with the increase in the (performance) points. It's a bit like PM here - the number of votes you get is tied to the number of XP's you've accumulated.

Create A New User
Node Status?
node history
Node Type: note [id://471983]
help
Chatterbox?
 [holli]: i got super stoned last night, had a large pizza, half an apple pie, two oranges, a banana, and half a bar of chocolate [holli]: oh and a handful of gummy bears [erix]: "macht die Kleine froh und die grosse ebenso!" [erix]: (or something like that) [holli]: you do know i'm a guy, right?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-11-22 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (316 votes). Check out past polls.

Notices?