Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > The error "Use of uninitialized value "

Reply
Thread Tools

The error "Use of uninitialized value "

 
 
geek
Guest
Posts: n/a
 
      03-22-2005
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";
}
################################################## #########################

 
Reply With Quote
 
 
 
 
phaylon
Guest
Posts: n/a
 
      03-22-2005
geek wrote:

> Any help will be appreciated.


Only if you rewrite that and use more Hashes.

--
http://www.dunkelheit.at/
codito, ergo sum.

 
Reply With Quote
 
 
 
 
Gary E. Ansok
Guest
Posts: n/a
 
      03-22-2005
In article <(E-Mail Removed) .com>,
geek <(E-Mail Removed)> wrote:
>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
--
Chaos reigns within.
Reflect, repent, and reboot.
Order shall return.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-22-2005
"geek" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> 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:umper;
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
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      03-22-2005
geek wrote:
> 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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-22-2005
"A. Sinan Unur" <(E-Mail Removed)> wrote in
news:Xns96217BE1FE637asu1cornelledu@127.0.0.1:

> 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.
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      03-22-2005
geek wrote:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Gary E. Ansok
Guest
Posts: n/a
 
      03-22-2005
In article <Xns962181BFAE8A6asu1cornelledu@127.0.0.1>,
A. Sinan Unur <(E-Mail Removed)> wrote:
>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
--
Any attempt to brew coffee with a teapot should result in the error code
"418 I'm a teapot". The resulting entity body MAY be short and stout.
-- RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      03-22-2005
A. Sinan Unur wrote:

> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Gary E. Ansok) wrote in news:d1pmg9$3u1$1
@naig.caltech.edu:

> In article <Xns962181BFAE8A6asu1cornelledu@127.0.0.1>,
> A. Sinan Unur <(E-Mail Removed)> wrote:
>>Incidentally, I had never before written
>>a function with the same name as a builtin, so I was pleasantly
>>surprised when

....
>>
>>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.


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error uninitialized value Wijnand Nijs Perl Misc 11 08-20-2009 11:40 PM
Perl error - uninitialized value has me stumped BradK Perl 0 04-10-2007 07:15 PM
Error "Use of uninitialized value " geek Perl Misc 2 03-23-2005 10:41 AM
Error "Use of uninitialized value " geek Perl Misc 2 03-22-2005 08:59 PM
Re: Use of uninitialized value in concatenation (.) or string Error Sukhbir Dhillon Perl 1 04-05-2004 02:31 AM



Advertisments