Jim Gibson wrote:
On 4/17/09 Fri Apr 17, 2009 1:50 PM, "Brian" <brian5432...@yahoo.co.uk>
scribbled:
Brian wrote:
oops, should read......
$Year_out = $Year_in;
while ($Year_out > 100) {$Year_out -= 100;}
if (($Year_out > 00) && ($Year_out <= 25)) {$string = $string1;}
if (($Year_out > 25) && ($Year_out <= 50)) {$Year_out -=
25;$string = $string2;}
if (($Year_out > 50) && ($Year_out <= 75)) {$Year_out -=
50;$string = $string3;}
if (($Year_out > 75) && ($Year_out <= 100)) {$Year_out -=
75;$string = $string4;}
You are doing quite a few redundant tests here. Note that after your while
loop, $Year_out cannot be greater than 100. Also, if $Year_out is greater
than 25, it must be greater than 0. In addition, when you are done,
$Year_out will be between 0 and 25. Therefore, you can take the value modulo
25 and rearrange the tests to be a little more efficient (I am assuming that
$Year_out is always >= zero):
while ( $Year_out > 100 ) {
$Year_out -= 100;
}
if ( $Year_out > 75 ) {
$string = $string4;
}elsif ( $Year_out > 50 {
$string = $string3;
}elsif ( $Year_out > 25 ) {
$string = $string2;
}elsif ( $Year_out > 0 ) {
$string = $string1;
}
$Year_out = $Year_out % 25;
Thanks for that, a better structure, but I don't understand the use of %
25 in this instance.
$year is actually always going to be >= zero , even if someone enters a
negative.
I haven't factored for the Julian/Gregorian changeover, so this prog
will calc year 1 as it would have been had todays calendar been in use
2000+ years ago.
A point I should make here is that I am actually working over 400 year
cycles and not 100, so...
100 = 400
75 = 300
50 = 200
25 = 100
but I suppose the results will be this same in your code above.
(I changed certain parameters to stop any robots from stealing my code)
;-)
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/