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.
From: Uri Guttman [mailto:[email protected]]
Sent: Friday, February 06, 2015 9:25 AM
To: [email protected]
Subject: Re: use perl format data
On 02/05/2015 08:07 PM, Wang, Zeng-Sheng (TS-GSD-China-ZZ) wrote:
Dear Shawn,
Please forgive me for my poor English explanations of my question, there are
three files, file 1, file 2 and file3.
File 1:
a b c
d e f
j p k
file 2:
x y z
q w e n
a s d
file 3:
1 2 3
3 4 5
8 9 2 1
I read file 1 to $a, file 2 to $b and file 3 to $c, I want to product file 4,
it contain file 1 , file 2 and file 3.
as several people have said, don't use $a and $b for variables. even in simple
examples like this. seriously don't use them.
File 4:
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
it seems you want the first line of each file, then the 2nd and then the third.
this is easy. i won't code it for you but i will give you some direction.
first, just read each file into an array of lines. you can do this in several
ways but you can work it out yourself first. we will help with any problems but
that is easy enough.
then remove the newlines of each line with chomp. it can do a whole array for
you in one call.
then get the first line of each array with shift and build up the first line of
the output. you can use the . op to append each line onto the variable like
this:
$out_line .= $line_from_file ;
then append a newline to that line. you don't need to output it to a file to
read it back in to $z.
do that for the other 3 lines. there are ways to do this with loops but in your
case stick with 3 copies of similar code. looping will likely confuse you as
you will need to know about arrays of arrays and such. you seem to be a early
beginner and aren't ready for perl data structures.
so code this up, test it out, debug it. when you are stuck next, post the
complete program and we can help more.
uri