CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
For Loops

 



SirAnvil
Deleted

Feb 3, 2001, 4:09 PM

Post #1 of 5 (472 views)
For Loops Can't Post

I'm not real good with loops (at all) so I need help. Here it goes.

I have a number of 100.
Then I get a random number from 0 to 75 and subtract it from that from 100.

Then, I have another number like 150 and make a random number between 0 and 75 and subtract if from 150.

This is simple so far. What I'm trying to do now is to keep doing this until one of them is equal to or less then zero. Then the other one will stop and display how much is left over.

So due to the randomization of the smaller number being subrated from the larger number either party could end with zero or less before the other. I can't figure out the loop to do this. Anyone be willing to not only help out but to explain so that I may learn it as well?



SirAnvil
www.christiangamers.org


kencl
User

Feb 4, 2001, 1:50 AM

Post #2 of 5 (469 views)
Re: For Loops [In reply to] Can't Post


Code
$Hundred = 100;   $OneFifty = 150; 
$keepgoing = 1; # true
while ($keepgoing) {
$Hundred = $Hundred - &Random(0, 75);
last if ($Hundred < 0);
$OneFifty = $OneFifty - &Random(0,75);
last if ($OneFifty < 0);
}
# determine which one fell below zero and sets $answer to the other one.
($Hundred < 0)?($answer = $OneFifty):($answer = $Hundred);
sub Random($low, $high) {
return a number between $low and $high # your randomizing routine goes in here
}

We don't modify $keepgoing within the loop, so we break out of the loop with the keyword last. This occurs as soon as either one of the variables falls below zero, so if $Hundred falls below zero first, the loop terminates immediately and $OneFifty does not go through another subtraction. We also know that if $OneFifty falls below zero first, (causing the loop to terminate) $Hundred is greater than zero (otherwise it would have already terminated the loop :)

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


SirAnvil
Deleted

Feb 4, 2001, 4:58 PM

Post #3 of 5 (462 views)
Re: For Loops [In reply to] Can't Post

So far so WONDERFUL. However the $onefifty is always returning 150 while the $hundred ends in a negative number. Could it be that my subroutine needs help? I checked to se if it's because the $hundred keeps finishing first by reducing the number of $onefifty to 40 and still get the same results on every return....

Here is what I got

$hundred = 100; $onefifty = 150;
$keepgoing = 1; #tru
while ($keepgoing) {
$hundred = $hundred - &hit(0, 75);
last if ($hundred < 0);
$onfifty = $onefifty - &hit(0, 75);
last if ($onefifty < 0);
}
($hundred < 0) ? ($answer = $onefifty) : ($answer = $hundred);

Here is my sub routine

sub hit($low, $high) {
$hit = rand(75);
$hit = int ($hit);
}

Then I print the results like sooo

print "$hundred\n";
print "$onefifty\n";

My resulst are like this

-37
150

or

-12
150

No matter what 150 is the same. Any Ideas?

SirAnvil
www.christiangamers.org


----edited for television----

STOP STOP...... Sorry, it's my bad spelling again. I mis-spelled a word in the loop. It's fixed now and works beautiful!!!!

Now here's a neato questions. Suppose I want to stop in the middle of the loop and ask a question like this

print "Would you like to continue?\n";
$continue = <>;

Then if $continue eq yes it continues again until it exits out or asks the question again
if $continue eq no then it stops right there. Mwahhh ha haaa....


(This post was edited by SirAnvil on Feb 4, 2001, 4:12 PM)


kencl
User

Feb 5, 2001, 9:40 PM

Post #4 of 5 (448 views)
Re: For Loops [In reply to] Can't Post

Are you running this from a terminal, or is it processing form input from a web page?

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


SirAnvil
Deleted

Feb 8, 2001, 2:55 PM

Post #5 of 5 (437 views)
Re: For Loops [In reply to] Can't Post

Right now I'm simply running from a terminal. I will be converting it to web forms later when I get the bugs and kinks worked and and working the way I want it.

SirAnvil
www.christiangamers.org

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives