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:
A little help I am stuck

 



nimdavirus
New User

Apr 21, 2005, 9:17 AM

Post #1 of 6 (943 views)
A little help I am stuck Can't Post

I have started on this script for my class and I am stuck. I know
I am and missing something but here please take a look. I am trying to get input from the user and then take all 10 numbers that are giving and do some math here. I want to take all the odd numbers that are giving and muiltiply them and the even sum them up. with a nice print out of all the numbers that where entered. Any help would be very nice. Thanks


Code
#!/usr/local/bin/perl -w 
#
$x_odd = 1; $x_sum = 0;


# Main program starts here
print "Please enter up to 10 random numbers. When you are done\n";
print "type quit, and let this Perl program do its magic. ^_^\n";
print "\n\n";
print "Number: ";



while ($x = <STDIN>) {
if ($x == "\n") {
$z = @numbers;
print "count equals $z";
foreach $x (@numbers) {
($x %2 == 0) ? $x_sum += $x : $x_odd *= $x;
print "$x, $x_odd, $x_sum\n" }
print "sum = $x_sum, prod = $x_odd\n";
exit;
}
chomp ($x);
push (@numbers, $x);
}


#while ($number = <STDIN>) {
# print "Number: ";
# chomp($number);
# # # # if ($number-2*int($number/2) == 0) {
#$even += $number;
# } elsif (abs($number-2*int($number/2)) == 1) {
#abs reutrns the absolute value of its argument
# $odd *= $number;
# }
# }

#print "Your numbers are: $number";
#print "\n\n The odd numbers multipled would be: $odd";
#print "\n\n The even numbers sum is: $even";
;



maximus
Novice

Apr 21, 2005, 10:25 AM

Post #2 of 6 (941 views)
Re: [nimdavirus] A little help I am stuck [In reply to] Can't Post

It is Mad upon in this forum to ask help for ur school work. But, here are a few pointers, since you are using -w, you must be seeing some errors. Fix those first and then check the setting of your variables.


nimdavirus
New User

Apr 21, 2005, 10:38 AM

Post #3 of 6 (940 views)
Re: [maximus] A little help I am stuck [In reply to] Can't Post

thats why I am not asking for you to do it for me. :) just where I am missing up. I am taking this class online. So I don't get any help at all. :( I have to read and therefore I have to then understand what I read. Really hard.


nimdavirus
New User

Apr 21, 2005, 11:18 AM

Post #4 of 6 (939 views)
Re: [nimdavirus] A little help I am stuck [In reply to] Can't Post

Okay I redid it. I am trying to get my sub to do the math. Then I can just take what it will assigns to the variables and the print them out. I am going on 7 hours here and I can't get it :(

Or am I am hosed up. Let me know thank you


Code
#!/usr/local/bin/perl -w 
#
#
# Main program starts here
print "Please enter up to 10 random numbers. When you are done\n";
print "type quit, and let this Perl program do its magic. ^_^\n";
print "\n\n";
print "Number: ";

while ($numbers = <STDIN>) {
print "Numbers: "; #Printing Name again to look pretty
if ($numbers eq "quit") {
# } foreach $numbers ($do_math) {
print "The numbers you entered where $numbers\n";
print "The sum of the even numbers are $odd\n";
print "The odd numbers nuiltiplied are $even\n";
}
}


sub math {

my ($math);
$odd =1; $even =0;
foreach $numbers (@number) {
($numbers %2 == 0) ? $odd += $numbers : $even *= $numbers;
}
return $math;
}



(This post was edited by nimdavirus on Apr 21, 2005, 11:33 AM)


KevinR
Veteran


Apr 21, 2005, 12:58 PM

Post #5 of 6 (935 views)
Re: [nimdavirus] A little help I am stuck [In reply to] Can't Post

just taking a quick look at your revised code, it looks like the array @number is never being populated with any data. I assume that must be the list of numbers being entered, so make sure you populate the array before trying to run the sub routine, which you never call anywhere in the script anyway. sub Math is not going to run without being called first. Wink

You will probably want to chomp the user input too each time a number is recieved or chomp the array before processing the data. I recommend you use"strict" too as that will assit you in coding your scripts properly.

#!/usr/local/bin/perl -w
use strict;
-------------------------------------------------


nimdavirus
New User

Apr 22, 2005, 4:30 AM

Post #6 of 6 (912 views)
Re: [KevinR] A little help I am stuck [In reply to] Can't Post

Yay I kinda got it! Only took me 16+ hours

Thanks


Code
# Main program starts here 
my(@number, $numbers);

print "Please enter up to 10 random numbers,\n";
print "and let this Perl program do its magic. ^_^\n";
print "\n\n";

$amount = 0;

do {

$amount++;

print"Number: ";

chomp($numbers = <STDIN>);

if ($numbers) {
push (@number, $numbers);
}

} until $amount >= 10; {

&math
}




sub math {



$even =1;

foreach $thisnumber (@number) {

# ($thisnumber %2 == 0) ? $odd += $thisnumber : $even *= $thisnumber;

if ($thisnumber-2*int($thisnumber/2) == 0) {
$odd += ($thisnumber);
print "$thisnumber is even\n";
} elsif (abs($thisnumber-2*int($thisnumber/2)) == 1) {
$even *= ($thisnumber);
print "$thisnumber is odd\n";
}


}
print "\n\nAll numbers entered were @number\n\n";

print "odd numbers added are = $odd\n\n";
print "even numbers muiltiplied are = $even\n\n";

}


~David


(This post was edited by nimdavirus on Apr 22, 2005, 4:54 AM)

 
 


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

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