Error "Use of uninitialized value "

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

  1. geek

    geek Guest

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

  2. geek wrote:

    > 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;
     
    Brian McCauley, Mar 23, 2005
    #2
    1. Advertising

  3. Brian McCauley wrote:

    > geek wrote:
    >
    >> I am getting the error "Use of uninitialized value" in the line "else {
    >> stat();}"
    >>
    >> Any help will be appreciated. Look below for the code .


    > 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.
     
    Brian McCauley, Mar 23, 2005
    #3
    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,254
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,277
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    6,020
    Joe Smith
    Jun 9, 2004
  4. geek
    Replies:
    21
    Views:
    220
    Sherm Pendley
    Mar 24, 2005
  5. geek
    Replies:
    2
    Views:
    110
Loading...

Share This Page