warning in Push statement.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studentmadhura05
    New Member
    • Nov 2006
    • 25

    warning in Push statement.

    Hi
    I am trying to Push one line at a time from three different arrays into a new array
    Here is a part of the code that is giving trouble:

    Code:
         my @result_line;
    
         while ($entries >=  0 ){
         @result_line = ();
         push @result_line, $result_line1[$entries];
         push @result_line, $result_line2[$entries];
         push @result_line, $result_line3[$entries];
         print "@result_line\n";
         $entries--;
     }

    The code gets compiled with rest of the code and also gives me proper result But I am getting following (warning) message each time it loops through the while loop:

    Use of uninitialized value in join or string at search_IP.plx (for the line with the Print statement)

    I have initialized the array @result_line then why is it happening?
    Please help.
    Thanks
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Originally posted by Studentmadhura0 5
    why is it happening?
    Because you push() one or more undefined values into @result_line.

    If that's not help enough, please post a short but complete program, that we can copy and run, and that demonstrates the issue.

    Comment

    • Studentmadhura05
      New Member
      • Nov 2006
      • 25

      #3
      Hi,
      I did not paste the lines where I have aleady declared and initialized the arrays that i am pushing.

      Code:
      my @result_line1 = grep (/lease/, @result);
      my @result_line2 = grep (/starts/, @result);
      my @result_line3 = grep (/ends/, @result);
      
      while ($entries >=  0 ){
           @result_line = ();
           push @result_line, $result_line1[$entries];
           push @result_line, $result_line2[$entries];
           push @result_line, $result_line3[$entries];
           print "@result_line\n";
           $entries--;
      }
      Actually there is no array or variable that is not initialized. Please help.

      Comment

      • GunnarH
        New Member
        • Nov 2006
        • 83

        #4
        That's still not a complete program.

        It's not about a variable, but at least one element in @result_line is undefined.
        Code:
        C:\home>type test.pl
        use warnings;
        my @array = ('blue', undef, 'yellow');
        print "@array\n";
        
        C:\home>test.pl
        Use of uninitialized value in join or string at C:\home\test.pl line 3.
        blue  yellow
        
        C:\home>

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Just because the arrays are initialized, does not mean that the scalar elements that you are trying to access are also initialized:

          Code:
          my @result_line1 = grep (/lease/, @result);
          my @result_line2 = grep (/starts/, @result);
          my @result_line3 = grep (/ends/, @result);
          
          warn "badness1" if $entries > $#result_line1;
          warn "badness2" if $entries > $#result_line2;
          warn "badness3" if $entries > $#result_line3;

          Comment

          Working...