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

Reply via email to