Current location: Hot Scripts Forums » Programming Languages » Perl » basic perl question, need help pls!


basic perl question, need help pls!

Reply
  #1 (permalink)  
Old 10-11-06, 01:12 AM
penguin_f4 penguin_f4 is offline
New Member
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question basic perl question, need help pls!

hi,

i'm taking my intro perl course, and am working on the first lab. i've got it all done with one last outstanding problem in my perl program.

the program takes in one or two integers per line, which may be signed. after you type "quit", it will return the average of all the integers entered.

right now, it will take in one or two integers per line, as it should, and it will correctly add in the unsigned ints and the positive signed ints for calculation. the last part, and the part that does not work, is taking in the negative signed ints and subtracting them in the calculations.

when it encounters a negative signed int, it gives this error:
Use of uninitialized value in subtraction (-) at Lab1_2.pl line 22, <STDIN> line 1.

the code is as follows. i will greatly appreciate any insight you guys may have to help me! in addition to fixing my problem, it'd be great if you guys might be able to give me pointers on my code and style. thanks in advance!!

Code:
$numSum = $totalNum = 0;
while((print "Enter line: "),($line = <STDIN>) !~ /^\s*quit\s*$/i)
{
	next if $line =~ /^\s*$/;
	if($line !~ /^\s*([+-]?)(\d+)\s*([+-]?\d*)?\s*$/)
	{
		print "Bad line.";
		next;
	}
	
	if(length($1))
	{
		if($1 =~ "-")
		{
			$numSum -= $2;
		}
		else
		{
			$numSum += $2;
		}
	}
	else
	{
		$numSum += $2;
	}
	
	$totalNum++;
	
	if(length($3))
	{
		if($3 =~ /([+-]?)(\d*)/)
		{
			if(length($1))
			{
				if($1 =~ "-")
				{
					$numSum -= $2;
				}
				else
				{
					$numSum += $2;
				}
			}
			else
			{
				$numSum += $2;
			}
			
			$totalNum++;
		}
	}
}

if($totalNum)
{
	print "\nAverage: ", $numSum / $totalNum, "\n";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-16-06, 07:10 PM
Rando Rando is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Wow you're overthinking it.

Code:
#!/usr/bin/perl -w

my $sum=0;
my $i=0;
while(<>){
  last if(/quit/);
  if(/^[[:digit:]+-]/){
    $i++;
    $sum += $_;
  } else {
    print "Bzzt!\n"
  };
}
my $avg = $sum/$i;
print $avg;
__________________
Full root access starting at $7.95/mo VPSLink

Last edited by Rando; 10-16-06 at 07:15 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
More info SLAYER18 Job Offers & Assistance 1 01-17-05 06:06 AM
Simple Perl Question whillers Perl 2 11-10-04 04:44 PM
Configuring perl newbie question? mickengland Perl 1 09-17-04 12:02 PM
Question about Perl Report - please help carlnasc Perl 1 04-14-04 06:04 AM
php visual basic perl cgi - outsource programming to india outsource_india General Advertisements 0 10-28-03 04:21 AM


All times are GMT -5. The time now is 01:49 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.