Hi Jim,

Thanks for your comments, I will improve my script and add logic for different 
lines in several files. 
In perl, I have a lot of knowledge to study.


Thanks again.


-----Original Message-----
From: Jim Gibson [mailto:jimsgib...@gmail.com] 
Sent: Friday, February 06, 2015 2:19 PM
To: Wang, Zeng-Sheng (TS-GSD-China-ZZ); beginners@perl.org
Subject: Re: use perl format data


> On Feb 5, 2015, at 6:53 PM, Wang, Zeng-Sheng (TS-GSD-China-ZZ) 
> <zengsheng.w...@hp.com> wrote:
> 
> Dear Uri,
>  
> First thanks for your kindly help and a read-friendly instruction, I will not 
> use $a and $b for variables. According to your explanation, I finish the code 
> as below:
> #!/usr/bin/perl -w
>   2 use strict;
>   3 open(FH,'<',"file1");
>   4 my @first_file = <FH>;
>   5 close (FH);
>   6
>   7 open(FH,'<',"file2");
>   8 my @second_file = <FH>;
>   9 close (FH);
> 10
>  11 open(FH,'<',"file3");
> 12 my @third_file = <FH>;
> 13 close (FH);
> 14
>  15 chomp @first_file;
> 16 chomp @second_file;
> 17 chomp @third_file;
> 18
>  19 my ($out_line,$number);
> 20 $number = @first_file;
> 21 $out_line='';
> 22 for(0..$number){
> 23         $out_line.= shift @first_file;
> 24         $out_line.= "\t".shift @second_file;
> 25         $out_line.="\t".shift @third_file;
> 26         $out_line.="\n";}
> 27
>  28 print $out_line;
>  
> I realize my target, however, there are some warnings in the prompt, how can 
> I eliminate them? Why?
> perl format.pl
> Use of uninitialized value in concatenation (.) or string at format.pl line 
> 23.
> Use of uninitialized value within @second_file in concatenation (.) or string 
> at format.pl line 24.
> Use of uninitialized value within @third_file in concatenation (.) or string 
> at format.pl line 25.
> a b c       x y z       1 2 3
> d e f       q w e n 3 4 5
> j p k        a s d       8 9 2 1
>  
> Thank you very much again.

In line 20, @first_file is evaluated in scalar context, which is the number of 
elements in the array, and assigned to $number. In line 22, your for loop is 
iterated for values 0 through $number. This results in $number+1 iterations. 
Therefore, during the last iteration, all of the elements of all three arrays 
have been exhausted, and your program is attempting to concatenate three 
undefined values to $out_line. Hence the three warning messages.

The easiest way to fix this is to iterate on values (0..$#first_file). 
$#first_file is the maximum index of the @first_file array.

In the longer term, you may want to add logic to your program to handle the 
case where the files have differing numbers of lines or you have more than 
three files.

There are numerous other improvements you can make to this program, if you are 
interested.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to