CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
output in separate files

 



David4
Deleted

Mar 16, 2000, 11:54 PM

Post #1 of 3 (632 views)
output in separate files Can't Post

Hi,

How can I get three output files? outfile1.txt for the first time of the loop,
outfile2.txt for the second time of the loop
and outfile3.txt for the third time of the loop. Thank you!!!

David

Here's the program:

$tall = 'fruitfile2.txt';

open(OUT, '>outfile.txt');

open(TREES, "<$tall") or die "Couldn't open the fruitfile2.txt file for writing. !\n";

while (<TREES> )
{
push @fruits,$_;
}
close(TREES);


$prefer = 'testfile.txt';
open(LEAF, "$prefer") or die "Couldn't open the ". "testfile.txt file for writing.\n";
@fileArr = <LEAF>;
close LEAF;

for ($i=0; $i<=2; $i++){
foreach (@fileArr){
chomp;
($tmpStr= $_)=~ s/fruits/@fruits[$i]/g;
print OUT "$tmpStr\n";
}
}

fruitfile2.txt:
big red apples
large juicy oranges
small firm peaches

testfile.txt:
I like to eat fruits. I think fruits are delicious.
How many fruits do you want?
I like this line to be printed too.


Cure
User

Mar 17, 2000, 11:35 AM

Post #2 of 3 (632 views)
Re: output in separate files [In reply to] Can't Post

Hi

This code will do what your asking for:

$prefer = 'testfile.txt';
$tall = 'fruitfile2.txt';

open(TREES, "<$tall") or die "Couldn't open the fruitfile2.txt file for writing. !\n";
@fruits = <TREES>;
close(TREES);


open(LEAF, "$prefer") or die "Couldn't open the ". "testfile.txt file for writing.\n";
@fileArr = <LEAF>;
close LEAF;

open(ONE, '>outfile1.txt');
foreach (@fileArr){
chomp;
($tmpStr=$_)=~ s/fruits/@fruits[0]/g;

print ONE "$tmpStr\n";
}
close(ONE);

open(TWO, '>outfile2.txt');
foreach (@fileArr){
chomp;
($tmpStr=$_)=~ s/fruits/@fruits[1]/g;

print TWO "$tmpStr\n";
}
close(TWO);


open(THREE, '>outfile3.txt');

foreach (@fileArr){
chomp;
($tmpStr=$_)=~ s/fruits/@fruits[2]/g;

print THREE "$tmpStr\n";
}

close(THREE);


Cure


David4
Deleted

Mar 17, 2000, 1:11 PM

Post #3 of 3 (632 views)
Re: output in separate files [In reply to] Can't Post

Hi Cure,

Thanks! Actually, I was looking for a way to have the three files created automatically. If there was a need for 100 separate files then the way you have it would be very tedious. I guess I was not clear in my question.

Can you take another look?


David

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives