File operation and Concatenation

G

Gokul

Hi all,

I need to perform a concatenation with the contents of a file(s) after
opening the file(s).
I am pretty new to PERL and learning its potentials.

The operation I need to perform is outlines as follows:

File A has the following contents:[abc,def, ghi .... etc are strings
listed one by one in the file]

abc
def
ghi
jkl
mno

File B has the following contents:[Test_1,Test_2 .... etc are strings
listed one by one in the file]

Test_1
Test_2
Test_3
Test_4
Test_5

I want PERL to concatenate and generate strings for me as follows:[We
can create a new file, write strings to it and save it]

Test_1 abc def ghi jkl mno
Test_2 abc def ghi jkl mno
Test_3 abc def ghi jkl mno
Test_4 abc def ghi jkl mno
Test_5 abc def ghi jkl mno


Please help me through this.

Thanks
 
J

Jürgen Exner

Gokul said:
I need to perform a concatenation with the contents of a file(s) after
opening the file(s).
I am pretty new to PERL and learning its potentials.

The operation I need to perform is outlines as follows:

File A has the following contents:[abc,def, ghi .... etc are strings
listed one by one in the file]

abc
def
ghi
jkl
mno

File B has the following contents:[Test_1,Test_2 .... etc are strings
listed one by one in the file]

Test_1
Test_2
Test_3
Test_4
Test_5

I want PERL to concatenate and generate strings for me as follows:[We
can create a new file, write strings to it and save it]

Test_1 abc def ghi jkl mno
Test_2 abc def ghi jkl mno
Test_3 abc def ghi jkl mno
Test_4 abc def ghi jkl mno
Test_5 abc def ghi jkl mno


Please help me through this.

open(*) file A, slurp in the whole file into a string(**) and
replace(***) the newlines with a space.
Then simply open(*) file B, loop (****) through it line by line
(*****)and after chomp'ing (******)each line write (*******) it, the
combined line from A, and a new newline into your target file.

*: perldoc -f open
**: perldoc -q "read in an entire file"
***: perldoc -f s
****: perldoc perlsyn (and look for while())
*****: perldoc perlop, section I/O operators, <>
******: perldoc -f chomp
*******: perldoc -f print

jue
 
S

sln

Hi all,

I need to perform a concatenation with the contents of a file(s) after
opening the file(s).
I am pretty new to PERL and learning its potentials.

The operation I need to perform is outlines as follows:

File A has the following contents:[abc,def, ghi .... etc are strings
listed one by one in the file]

abc
def
ghi
jkl
mno

File B has the following contents:[Test_1,Test_2 .... etc are strings
listed one by one in the file]

Test_1
Test_2
Test_3
Test_4
Test_5

I want PERL to concatenate and generate strings for me as follows:[We
can create a new file, write strings to it and save it]

Test_1 abc def ghi jkl mno
Test_2 abc def ghi jkl mno
Test_3 abc def ghi jkl mno
Test_4 abc def ghi jkl mno
Test_5 abc def ghi jkl mno


Please help me through this.

Thanks

Why do you obfuscate rotating a 5x1 to a 1x5 per Test_# getting a 5x5
of duplicate Tests_?
Does it bother you that Test_1 through Test_5 are identical?
Whats the significance of 5?
Maybe you just want to rotate (pivot) an order 5 column.
The dup's are baffeling.

I guess you know how to read files, so catenate should be easy:
(untested)

while ( ($lineA=<$fhA>) && ($lineB=<$fhB>) )
{
chomp $lineA; chomp $lineB;
push @pA,$lineA; push @pB,$lineB;
$order++;
if ($order == 5)
{
while ( my $test = shift @pB ) {
print $fhC "$test @pA\n";
}
@pA = ();
$order = 0;
}
}


sln
 
G

Gokul

I just gave FIVE instances to illustrate my purpose only.
FIVE is insignificant.
File A and File B might have a lot of strings in them.10s or 100s.
But the string catenation plays an important role.
However your idea was thought-Provoking.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top