The error "Use of uninitialized value "

G

geek

Hi all,

I am getting this error in my perl script at the line "else {stat();)"
See the code at the bottom.

Any help will be appreciated.
Thanks,
MJ
########################################################

if( param('Get')){ date();}
else { stat();}

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){
@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;
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 $avg;
my $finalAvg=0;
open(handGrade,"./gradeFile") or die "gradeFile cannot be opened";
my @avgarr = <handGrade>;
my $tmpAvg;
foreach $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";
}
###########################################################################
 
G

Gary E. Ansok

Hi all,

I am getting this error in my perl script at the line "else {stat();)"
See the code at the bottom.

if( param('Get')){ date();}
else { stat();}

sub stat {

Perl already has a function called stat(), and I suspect that it is
getting called instead of the subroutine you wrote.

Try renaming your subroutine.

Gary
 
A

A. Sinan Unur

I am getting this error

which error?
in my perl script at the line "else {stat();)"
See the code at the bottom
....

if( param('Get')){ date();}
else { stat();}

Please post a short but complete script that others can easily run.
sub stat {

perldoc -f stat

What, oh what, is the purpose of using the name of a perl built-in?
print header(), start_html("The Statistics for Administrator");
open(fileHandle,"./outputFile") or die "The file cannot be opened";

Why is the input file called "outputFile"?

Do include the reason for the failure as well as the name of the file in
your error messages.
my @fileData = <fileHandle>;

There is no need to slurp the file if you are going to process it line-
by-line anyway.
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;

Whenever you find yourself writing tedious stuff like this, please take
a moment to actually think about the problem you are attempting to
solve.

You have provided no information on the format of the data you are
trying to process, so it is really hard to figure out what you are
trying to do, but I suspect the following noise
foreach my $word(@fileData){
@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";
}

can be replaced with:

#! /usr/bin/perl

use strict;
use warnings;

sub calc_stats {
my $filename = shift;
open my $fh, '<', $filename
or die "Cannot open $filename: $!";

my %stats;

while(my $line = <$fh>) {
my @words = split / /, $line;
++$stats{$_} for @words;
}
return \%stats;
}

use Data::Dumper;
print Dumper(calc_stats('outputFile'));
__END__

It is now up to you to fix the rest of the jumble. In the process, you
might also discover what "this" error is. "Use of uninitialized value"
is a warning, not an error. It is too tedious to try and figure out
which part of your code is causing the warning to be emitted.
my $noLine;
open( fileHandle,"<./outputFile");
$noLine++ while <fileHandle>;

What is the purpose of opening the same file again (especially since you
have not closed it yet, to re-read it in its entirety. I have a sneaking
suspicion (which I cannot be bothered to test, but you should) that no
lines are being read from the file as it is at EOF now. Hence $noLine is
uninitialized etc.

Have you read the posting guidelines for this group?


Sinan
 
J

Jeff

geek said:
I am getting this error in my perl script at the line "else {stat();)"
See the code at the bottom.

Any help will be appreciated.
Thanks,
MJ
########################################################

if( param('Get')){ date();}
else { stat();}

[Snipping ugly code written by someone who doesn't know what a hash is]

stat() is a perl internal function that requires a value be passed into
it, or it defaults to using $_ (see perldoc -f stat). I'm guessing the
bad practice here of writing your own function with the same name as a
perl internal causes your error.

I'd also suggest reading perldoc perldata

~Jeff
 
A

A. Sinan Unur

I have a sneaking suspicion

This particular one was wrong. Incidentally, I had never before written
a function with the same name as a builtin, so I was pleasantly
surprised when

#!/usr/bin/perl

use strict;
use warnings;

sub stat {
print "stat\n";
}

stat();

__END__

produced:

D:\Home> s
Ambiguous call resolved as CORE::stat(), qualify as such or use & at D:
\Home\s.pl line 10.
Use of uninitialized value in stat at D:\Home\s.pl line 10.

which gives a very good explanation of the problem. Why did you waste
our time?

Sinan.
 
S

Sherm Pendley

geek said:
I am getting this error in my perl script at the line "else {stat();)"
See the code at the bottom.

Rename your stat() function. It conflicts with the builtin function of the
same name.

sherm--
 
G

Gary E. Ansok

Incidentally, I had never before written
a function with the same name as a builtin, so I was pleasantly
surprised when

#!/usr/bin/perl

use strict;
use warnings;

sub stat {
print "stat\n";
}

stat();

__END__

produced:

D:\Home> s
Ambiguous call resolved as CORE::stat(), qualify as such or use & at D:
\Home\s.pl line 10.
Use of uninitialized value in stat at D:\Home\s.pl line 10.

which gives a very good explanation of the problem. Why did you waste
our time?

In the original poster's code, the call to stat() was *before* the
definition of his own stat subroutine.

In that case, the built-in stat() is called and no warning is produced.
(Probably one should be, but that's a different issue and not the
original poster's fault.)

Gary
 
S

Sherm Pendley

A. Sinan Unur said:
which error?

Don't be ridiculous. The error is in the subject. Complaining because he
didn't repeat it in the message body is petty.
It is too tedious to try and figure out
which part of your code is causing the warning to be emitted.

It took me all of three seconds to realize that his stat() function
conflicted with the builtin. The only thing that's tedious here is reading
your pedantic line-by-line bitching about points that aren't one bit
relevant to the question.
Have you read the posting guidelines for this group?

Have you? I distinctly recall something being said there about posting
helpful responses, not attacks.

Posting an answer to the question, while adding a few minor points of style,
would have been helpful. Posting page after page of moaning about trivia,
and then refusing to actually answer the question because it's "too
tedious" to do so, was not.

sherm--
 
A

A. Sinan Unur

(e-mail address removed) (Gary E. Ansok) wrote in @naig.caltech.edu:
In the original poster's code, the call to stat() was *before* the
definition of his own stat subroutine.

Ah! I missed that.
In that case, the built-in stat() is called and no warning is
produced. (Probably one should be, but that's a different issue and
not the original poster's fault.)

Indeed, you are right.

Thank you for the correction.

Sinan.
 
A

A. Sinan Unur

Don't be ridiculous. The error is in the subject. Complaining because
he didn't repeat it in the message body is petty.

Oh, but I am being petty in a different way: I was complaining that the
message was a "warning", not an "error" as I pointed out later in my
response.
It took me all of three seconds to realize that his stat() function
conflicted with the builtin.

Well, I have never written a function with the same name as a builtin. I
assumed a more verbose warning, such as:

Ambiguous call resolved as CORE::stat(), qualify as such or use & at ...

would have been produced if the builtin was being called rather than his
version. As Gary Ansok pointed out, the warning is only produced if the
user defined function is in scope, but I did not realize it.
The only thing that's tedious here is reading your pedantic line-by-
line bitching about points that aren't one bit relevant to the
question.

I had a theory which was wrong. I stated the theory. I thought the
warning was being caused by one of the of the variables that were being
assigned to in convoluted ways. It was hard to test this because the
script the OP posted was not self-contained at all.
Posting an answer to the question, while adding a few minor points of
style, would have been helpful. Posting page after page of moaning
about trivia, and then refusing to actually answer the question
because it's "too tedious" to do so, was not.

I did answer the question. My answer turned out to be wrong. All the
moaning was emitted in the process of trying to reduce the code to
something I could comprehend.

Sinan.
 
T

Tad McClellan

Sherm Pendley said:
Don't be ridiculous. The error is in the subject.


There is no error in the subject.

Complaining because he
didn't repeat it in the message body is petty.


Complaining because he called a warning an error is not petty,
there is an important difference.
 
S

Sherm Pendley

Tad said:
Complaining because he called a warning an error is not petty,

A cryptic two-word question like the one above leaves the OP with a vague
impression that he's done something wrong, but does nothing to tell him
what that was, or help him learn to do better next time.

A. Sinan *could* have chosen to be helpful and point out that the text in
the subject was a warning. He chose not to. Instead, he chose to post a
snide two-word comment.

The regulars here can obviously read between the lines to "get" the joke,
but the OP hasn't a prayer of doing so. The comment wasn't intended to be
helpful or constructive, it was intended to make the OP look foolish, so as
to provide a source of amusement for some of the more childish regulars
here.

I call that petty.

sherm--
 
T

Tad McClellan

Sherm Pendley said:
A cryptic two-word question like the one above leaves the OP with a vague
impression that he's done something wrong,


Like posting horridly formatted code?

Like posting the same question 3 times a few minutes apart?

but does nothing to tell him
what that was, or help him learn to do better next time.


He might consider being more respectful of other people's time.

A. Sinan *could* have chosen to be helpful and point out that the text in
the subject was a warning. He chose not to. Instead, he chose to post a
snide two-word comment.


You reap what you sow.

(though I must agree that A. Sinan does too-often go overboard
on the harshness.
)

The comment wasn't intended to be
helpful or constructive, it was intended to make the OP look foolish,


You're right, there was no need to point that out, the OP did
a fine job of making that rather obvious.

I call that petty.


OK.
 
A

A. Sinan Unur

A. Sinan *could* have chosen to be helpful and point out that the text
in the subject was a warning.

I do recommend that you read the full post before making such claims.

Just for the record:

+> +
+>> +>>
+>>> I am getting this error
+>>
+>> which error?
+
+...
+
+>> It is now up to you to fix the rest of the jumble. In the process,
+>> you might also discover what "this" error is. "Use of uninitialized
+>> value" is a warning, not an error.

Sinan.
 
S

Sherm Pendley

A. Sinan Unur said:
I do recommend that you read the full post before making such claims.

What makes you believe I didn't?
Just for the record:

+>> It is now up to you to fix the rest of the jumble. In the process,
+>> you might also discover what "this" error is. "Use of uninitialized
+>> value" is a warning, not an error.

If you wanted to help, you could have posted this simple three-line response
and been done with it. You didn't - you chose to amuse yourself by slapping
the OP around for several pages first. That was rude, petty, and entirely
unnecessary, and the fact that you eventually got around to reluctantly
offering a little bit of real help doesn't change that.

sherm--
 
B

Brian McCauley

Sherm said:
If you wanted to help, you could have posted this simple three-line response
and been done with it. You didn't - you chose to amuse yourself by slapping
the OP around for several pages first. That was rude, petty, and entirely
unnecessary, and the fact that you eventually got around to reluctantly
offering a little bit of real help doesn't change that.

You are apparently confusing "answering the question the OP thought was
most important" with "offering real help". The two are not the same.
Sinan's suggestions were all real help. It is almost exactly what I
would have posted. Come to think of it is almost exact how I _did_
respond when the OP rudely reposted exactly the same question as a new
thread.

It is unkind, petty and pedantic to take the attitude "you didn't know
what question to ask so I'll just literally answer the question you
asked and let you continue to flounder".
 
T

Tassilo v. Parseval

Also sprach Brian McCauley:
You are apparently confusing "answering the question the OP thought was
most important" with "offering real help". The two are not the same.
Sinan's suggestions were all real help.

However, the more you wrap the "real help" in attitude, the more likely
it is that the "real help" is overlooked. Sinan deliberately chose to
give his help in a face-threatening manner. That's not the way I was
brought up. In fact, it is this kind of postings that will increase the
noise level in this group.

There's a couple of things that seem to trigger certain reflexes among
some of the regulars here: Mistaking a warning for an error, the word
"PERL", etc. It's not that those things should be considered capital
breaches. Posters here should be given the benefit of the doubt. Point
out the mistakes they made but do it in a way that doesn't make the OPs
feel stupid afterwards.
It is unkind, petty and pedantic to take the attitude "you didn't know
what question to ask so I'll just literally answer the question you
asked and let you continue to flounder".

I doubt that this was Sherm's suggestion.

Tassilo
 
S

Sherm Pendley

Brian said:
You are apparently confusing "answering the question the OP thought was
most important" with "offering real help".

Nonsense. I said nothing whatsoever about *what* questions A. Sinan chose to
answer. I commented on the manner in which he chose to answer them.

Comments like his "what error?" are not helpful. How could the OP reasonably
be expected to infer from those two words that the message he received was
a warning, rather than an error?
It is unkind, petty and pedantic to take the attitude "you didn't know
what question to ask so I'll just literally answer the question you
asked and let you continue to flounder".

Well then, it's a good thing I didn't take any such attitude.

sherm--
 
S

Sherm Pendley

Tassilo said:
However, the more you wrap the "real help" in attitude, the more likely
it is that the "real help" is overlooked.

That's an excellent point, and it's often overlooked. It's not simply a
matter of civility, although that's obviously nice to have in a group like
this - it's also a matter of effectiveness.

A comment that's made in a hostile tone, whether the hostility is real or
imagined, tends to trigger a defensive response in the listener. Quite
often, that defensiveness translates to not only resistance to the
messenger, but also to the message.

To put it more bluntly, someone who thinks you're a acting like a jerk isn't
likely to ignore your attitude towards him and focus on the facts. He's far
more likely to ignore both.

sherm--
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top