The error "Use of uninitialized value "

Discussion in 'Perl Misc' started by geek, Mar 22, 2005.

  1. geek

    geek Guest

    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";
    }
    ###########################################################################
    geek, Mar 22, 2005
    #1
    1. Advertising

  2. geek

    phaylon Guest

    geek wrote:

    > Any help will be appreciated.


    Only if you rewrite that and use more Hashes.

    --
    http://www.dunkelheit.at/
    codito, ergo sum.
    phaylon, Mar 22, 2005
    #2
    1. Advertising

  3. In article <>,
    geek <> 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.
    Gary E. Ansok, Mar 22, 2005
    #3
  4. "geek" <> wrote in
    news::

    > 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
    A. Sinan Unur, Mar 22, 2005
    #4
  5. geek

    Jeff Guest

    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
    Jeff, Mar 22, 2005
    #5
  6. "A. Sinan Unur" <> 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.
    A. Sinan Unur, Mar 22, 2005
    #6
  7. 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
    Sherm Pendley, Mar 22, 2005
    #7
  8. In article <Xns962181BFAE8A6asu1cornelledu@127.0.0.1>,
    A. Sinan Unur <> 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
    Gary E. Ansok, Mar 22, 2005
    #8
  9. 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
    Sherm Pendley, Mar 22, 2005
    #9
  10. (Gary E. Ansok) wrote in news:d1pmg9$3u1$1
    @naig.caltech.edu:

    > In article <Xns962181BFAE8A6asu1cornelledu@127.0.0.1>,
    > A. Sinan Unur <> 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.
    A. Sinan Unur, Mar 22, 2005
    #10
  11. Sherm Pendley <> wrote in
    news::

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


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


    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.
    A. Sinan Unur, Mar 22, 2005
    #11
  12. Sherm Pendley <> wrote:
    > A. Sinan Unur wrote:
    >
    >> which error?

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 22, 2005
    #12
  13. Tad McClellan wrote:

    > Sherm Pendley <> wrote:
    >> 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.

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 22, 2005
    #13
  14. Sherm Pendley <> wrote:
    > Tad McClellan wrote:
    >
    >> Sherm Pendley <> wrote:
    >>> 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.

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



    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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 22, 2005
    #14
  15. Sherm Pendley <> wrote in
    news::

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

    +> "A. Sinan Unur" <> wrote in
    +> news:Xns96217BE1FE637asu1cornelledu@127.0.0.1:
    +
    +>> "geek" <> wrote in
    +>> news::
    +>>
    +>>> 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.
    A. Sinan Unur, Mar 23, 2005
    #15
  16. A. Sinan Unur wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 23, 2005
    #16
  17. Sherm Pendley wrote:

    > A. Sinan Unur wrote:
    >
    >>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.


    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".
    Brian McCauley, Mar 24, 2005
    #17
  18. Also sprach Brian McCauley:

    > Sherm Pendley wrote:
    >
    >> A. Sinan Unur wrote:
    >>
    >>>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.

    >
    > 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
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Mar 24, 2005
    #18
  19. Brian McCauley wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 24, 2005
    #19
  20. Tassilo v. Parseval wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Mar 24, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,223
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,233
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    5,986
    Joe Smith
    Jun 9, 2004
  4. geek
    Replies:
    2
    Views:
    144
    Brian McCauley
    Mar 23, 2005
  5. geek
    Replies:
    2
    Views:
    97
Loading...

Share This Page