Error "Use of uninitialized value "

G

geek

Hi all,

I am getting the error "Use of uninitialized value" in the line "else {
stat();}"

Any help will be appreciated. Look below for the code .

Thanks,
MJ

#########################################################
sub stat {
print header(), start_html("The Statistics for Administrator");
open(fileHandle,"./outputFile") or die "The file cannot be opened";
my @fileData = <fileHandle>;
my $count1=0;
my $count2=0;
my $count3=0;
my $count4=0;
my $count5=0;
my $count6=0;
my $count7=0;
my $count8=0;
my $count9=0;
my $countt=0;
# my @array;
foreach my $word(@fileData){
print "******$word";
my @array=split(/ /, $word);

foreach my $element(@array){
if($element eq "CS5375"){
$count1++;
}
if($element eq "cd"){
$count2++;
}
if($element eq "cp"){
$count3++;
}
if($element eq "vim"){
$count4++;
}
if($element eq "%ls-l"){
$count5++;
}
if($element eq "man"){
$count6++;
}
if($element eq "perl-v"){
$count7++;
}
if($element eq "all"){
$count8++;
}
if($element eq "finger"){
$count9++;
}
if($element eq "ftp\n"){
$countt++;
# print "hello";
}
}
}

my $noLine=0;
open( fileHandle,"<./outputFile");
$noLine++ while <fileHandle>;
print "Number of lines in the file is $noLine";
my $noUsers= $noLine/2;
print br;
print "Number of users is $noUsers";
print br;
my $finalAvg=0;
open(handGrade,"./gradeFile") or die "gradeFile cannot be opened";
my @avgarr = <handGrade>;
my $tmpAvg;
foreach my $avg(@avgarr){
$tmpAvg="$finalAvg"+"$avg";
$finalAvg=$tmpAvg/2;
}
print "The average is $finalAvg";
print br;
my $percentage1=($count1/$noUsers)*100;
print " The percentage for first question is $percentage1";
my $percentage2=($count2/$noUsers)*100;
print br;
#####################################################################
 
B

Brian McCauley

geek said:
Hi all,

I am getting the error "Use of uninitialized value" in the line "else {
stat();}"

Any help will be appreciated. Look below for the code .

As others have pointed out that line is not in the code.

I will however point out some other problems with your code.
#########################################################
sub stat {

It is generally a good idea to avoid using the names of Perl built-ins
for your subroutines.
print header(), start_html("The Statistics for Administrator");
open(fileHandle,"./outputFile") or die "The file cannot be opened";

You should include the error in the error message.
my @fileData = <fileHandle>;

Why slurp?
my $count1=0;
my $count2=0;
my $count3=0;
my $count4=0;
my $count5=0;
my $count6=0;
my $count7=0;
my $count8=0;
my $count9=0;
my $countt=0;

Whenever you find yourself typing something like the above a big light
should go on in your mind and flash the word "agregate". It should burn
so brightly it causes you pain.
# my @array;
foreach my $word(@fileData){
print "******$word";
my @array=split(/ /, $word);

Why create a variable to only use it once?
foreach my $element(@array){
if($element eq "CS5375"){
$count1++;
}
if($element eq "cd"){
$count2++;
}
if($element eq "cp"){
$count3++;
}
if($element eq "vim"){
$count4++;
}
if($element eq "%ls-l"){
$count5++;
}
if($element eq "man"){
$count6++;
}
if($element eq "perl-v"){
$count7++;
}
if($element eq "all"){
$count8++;
}
if($element eq "finger"){
$count9++;
}
if($element eq "ftp\n"){
$countt++;

By now the light in your mind should burn so brightly that you are
writhing on the floor in pain.

my %count;
while (<fileHandle>) {
for ( split ) {
$count{$_}++
}
}
# print "hello";
}
}
}

my $noLine=0;
open( fileHandle,"<./outputFile");
$noLine++ while <fileHandle>;

Oh my $DEITY! You both slurp the file and then also read it again!

my $noLine = @fileData;

Or if you deceided not to slurp then just put the $noLine++ into the
loop where you read the file the first time.
print "Number of lines in the file is $noLine";
my $noUsers= $noLine/2;
print br;
print "Number of users is $noUsers";
print br;
my $finalAvg=0;
open(handGrade,"./gradeFile") or die "gradeFile cannot be opened";
my @avgarr = <handGrade>;
my $tmpAvg;

You are suffering from premature declaration. This variable is only used
within the loop body and does not need to carry a value forward from
one iteration to the next. This being the case it should be declared
within the loop body.
foreach my $avg(@avgarr){
$tmpAvg="$finalAvg"+"$avg";

The + operator works on numbers. Why are you forcing Perl to convert
from number to string and back again?
$finalAvg=$tmpAvg/2;

Perl can use parenteses in arrithmetic expressions - no need for a
temporary variable.

$finalAvg=($finalAvg+$avg)/2;

This is an interesting weighted average you are calculating.

Half the last grade plus a quarter of the penultimate plus an eighth of
the prepenultimate and so on.

Is that really what you wanted?
print "The average is $finalAvg";
print br;
my $percentage1=($count1/$noUsers)*100;

Using a simple hash approach this becomes.

my $percentage1=($count1{CS5375}/$noUsers)*100;
 
B

Brian McCauley

I will however point out some other problems with your code.

[ snip large waste of my time ]

All I'm doing here is repeating the good advice already posted by others
because the OP started duplicate threads.

Please do not do that it is selfish.

If you really think your post has been lost (you should wait a few hours
at least) you should mention in your re-post that it is a re-post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top