Hi,
Well I am getting this warning while running my script which is attached:Use of uninitialized value in concatenation (.) or string at aj.pl
in this line : print OUTFILE"$address[$k]|" as when i remove this line I don't get any warnings:
Can someone please tell me what is wrong with it:
Thanks
Aj

Here is my code:

#!/usr/bin/perl
use warnings;
use strict;

 my $input = 'try_file.txt';
 my $output = 'try_file_pipe.txt'; 
 my $i=0;
 my $lines="";
 our(@aj1,@aj2,@aj3,@aj4,@aj5);
 my @id=();
 my @author= ();
 my @text=();
 my @address=(); 
  open (INFILE, "$input") or die "Couldn't open $input for reading: $!\n";
  open (OUTFILE, "> $output") or die "Couldn't open $output for writing: $!\n";

  my @file1 =<INFILE>;
    foreach $lines(@file1) 
     {
       chomp($lines);    
         if($lines =~m/ID:/)
           { 
	     $lines=~s/\s+$//g;
	     @aj1=split(/:/,$lines);
             push(@id,$aj1[1]);
           }
      elsif ($lines =~m/Author/)
         {
	   $lines=~s/\s+$//g;
           @aj4=split(/:/,$lines);
	   push(@author,$aj4[1]);
         }
      elsif ($lines =~m/Address/)
         {  
	   $lines=~s/\s+$//g;
           @aj5=split(/:/,$lines);
	   push(@address,$aj5[1]);
         }
      elsif ($lines =~m/^\\/)
	 {
           $i=$i+1;
         }
      else
         {
           push(@text,"$lines");
	 }
     
           if ($i==1) 
	   {
            $i=0;( #no warnings 'uninitialized'; #of course when I am using this I don't get any warnings, still I am curious to know what is wrong with it.)
           
               print OUTFILE"@id|";   
	       my $n = scalar @author;
	     for($k=0;$k<$n;$k++)
	       {
		print OUTFILE"$author[$k]|";
		print OUTFILE"$address[$k]|";
	       }
	
   	print OUTFILE"@text";
      		@id=();
      		@text=();
		@address=();
		@author=();
    }
 }
close (INFILE);
close(OUTFILE);
exit;

Edited 7 Years Ago by ajay_p5: n/a

Hi,
Well I am getting this warning while running my script which is attached:Use of uninitialized value in concatenation (.) or string at aj.pl
in this line : print OUTFILE"$address[$k]|" as when i remove this line I don't get any warnings:
Can someone please tell me what is wrong with it:
Thanks
Aj

for($k=0;$k<$n;$k++)
	       {
		print OUTFILE"$author[$k]|";
		print OUTFILE"$address[$k]|";
	       }

$k is not initialised, as you have use strict.

for (my $k=0;$k<$n;$k==)

should do the trick.

Cheers.

Hey sorry but i did initialize "$k" with "my" in my program and it doesn't make any difference.. do you have something else to suggest..Thanks

Hey sorry but i did initialize "$k" with "my" in my program and it doesn't make any difference.. do you have something else to suggest..Thanks

Ah, found it i think.

You seem to be using $i as a 'end-of-record' indicator. When $i is set to 1, we have reached the end of the record and print the collected data into the outfile.

However, without the $i=0, it is never reset!

So when we go to process the second record (second set of id: author: address:) , after we have collected the first field (id: for example), $i is still 1, and we want to print the WHOLE record to the outfile, but author and address have not been collected / initialised yet!

So, at the end of the print-output block, you have to reset $i to 0, to signal we have not reached the end of the next record yet.

Hope this is a bit clear.

Thanks but this doesn't make a difference as well..

Hmm, weird. Putting a $i = 0; after line 67 (instead of at line 52, but works there also) did the trick for me. And from the posting i saw that the $i = 0 you put in at line 52 also prevents the error, because it resets the end-of-record indicator.

Unless ofcourse there is no author or address filed in the input data.

Cheers.

hmm thanks i guess there are certain data in the input file which doesn't contain address..may be that's the prob..anyways thanks for ur suggestions

This article has been dead for over six months. Start a new discussion instead.