1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2006
Posts
1
Rep Power
0

#### Simple Integer Average

Here is the script I have so far.... the question is on the bottom. Any idea how to finish it?

#!/usr/bin/perl -w
\$count = 0;
\$avg = 0;
while ((print "enter line:"),(\$line = <STDIN>) !~ /^\s*quit\s*\$/i)
{
if(\$line =~ /^\s*\$/)
{
next;
}
if(\$line !~ /^\s*([+-]?\d+)\s*?([+-]?\d*)\s*\$/)
{
print "ERROR! Pls enter valid data:\n";
next;
}
print "\$1 \$2";
if(defined(\$2))
{
if(\$2 !~ /^[+-]?\d+\$/)
{
\$tot = \$1 ;

}
else
{
\$tot = \$2;
\$count++;

}

}
\$count++;

}
if(\$count != 0)
{
\$ave = \$tot/\$count;
print "The total numbers entered = \$count";
print "the average = \$ave";
}

**************************************************************************8
QUESTION BELOW

write a pearl script with a "quit" loop . On each line of input,the user may
enter 1 or 2 integer tokens which may be signed. Compute the average of
integers from the correctly entered lines. Use the same input rules as
problem #1. Present the average on the screen .. if there is one to be
computed!!

Dont worry about the number of digits displayed in the averaqge.
Hint:can use perl function defined().

Rules.
a) it will allow lines to be entered until the user enters "quit"
b)
If user enters an empty line ,simply repromt with no error message.
c)If the user enters an invalid line reprompt after giving error msg.

d)Valid lines will have the one or two tokens described and may have any
amount of whitespace before/between/after the two tokens.

e) After the user quits out of the loop, present all of the valid first
tokens from valid lines (one per line), then all of the valid second tokens
from the valid lines (one per line), and the number of valid lines.
2. OK, this is homework, and you couldn't be bothered to tell us where you think the problem is, or how you'd like to go about solving it ... c'mon, you would have had a better chance getting help if you made an effort ...
3. Happy birthday. Should be easy to explain to your professor.
Code:
`perl -e '@a;@b;while(chomp(\$i=<STDIN>)&&\$i!~m/quit/i){next unless \$i;if(\$i=~m/[^\d-\s]/){print"error\n";next}@i=split(/\s+/,\$i);my\$f;map{\$f+=\$_}@i;if(@i==2){print\$f/@i."\n";push@a,\$i[0];push@b,\$i[1];}}print map{"\$_\n"}"first",@a,"second",@b,"total",scalar @a'`
And yes - it works to the requirements of the assignment.