I have 2 files to read from, namely file1 and file2.
I want to insert the content of file1 to be the
2nd column of file2.
content of file1
a
b
c
d
e
content of file2
1 6 11
2 7 12
3 7 13
4 8 14
5 9 23
I want the output to be as follows
1 a 6 11
2 b 7 12
3 c 7 13
4 d 8 14
5 e 9 23
My code didn't return the result I want
#!/bin/perl
open(IN,"file1");
open(IN2,"file2");
open (OUT,"> output");
while($str=<IN2>)
{
@array=split(/\s+/,$str);
while($data=<IN>)
{
@box=split(/\s+/,$data);
foreach $item(@array)
{
splice (@box,2,0,"$array($item)");
print OUT "@box\n";
}
}
}
20050308 Janitored by Corion: Added code tags.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.