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:
separate files

 



David4
Deleted

Mar 22, 2000, 7:21 PM

Post #1 of 4 (1300 views)
separate files Can't Post

Hi,

How do I get the output in three separate files automatically ( going through some kind of loop) ?

Here's the program:

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

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


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


open(OUT, ">sample.txt");

for ($start=3; $start<6; $start++){
for ($i=0; $i<=2; $i++){
foreach (@fileArr){
chomp;

($tmpStr = $_) =~ s/fruits/@fruits[$i]/g;

$tmpStr =~ s/7/$start/g;
print OUT "$tmpStr\n"


}
}
}


fruitfile2.txt:

big red apples
large juicy oranges
small firm peaches


testfile3.txt:

I like to eat 7 fruits. John wants 7 fruits.
There are 7 fruits in the basket.
I like this line to be printed too.


The 3 output files that I like to see after some kind of loops are:

sample1.txt:
I like to eat 3 big red apples. John wants 3 big red apples
.
There are 3 big red apples in the basket.
I like this line to be printed too.

I like to eat 3 large juicy oranges. John wants 3 large juicy oranges.
There are 3 large juicy oranges in the basket.
I like this line to be printed too.

I like to eat 3 small firm peaches. John wants 3 small firm peaches.
There are 3 small firm peaches in the basket.
I like this line to be printed too.


sample2.txt:

I like to eat 4 big red apples. John wants 4 big red apples. There are 4 big red apples in the basket.
I like this line to be printed too.

I like to eat 4 large juicy oranges. John wants 4 large juicy oranges. There are 4 large juicy oranges in the basket.
I like this line to be printed too.

I like to eat 4 small firm peaches. John wants 4 small firm peaches.
There are 4 small firm peaches in the basket.
I like this line to be printed too.


sample3.txt:

I like to eat 5 big red apples. John wants 5 big red apples. There are 5 big red apples in the basket.
I like this line to be printed too.

I like to eat 5 large juicy oranges. John wants 5 large juicy oranges. There are 5 large juicy oranges in the basket.
I like this line to be printed too.

I like to eat 5 small firm peaches. John wants 5 small firm peaches.
There are 5 small firm peaches in the basket.
I like this line to be printed too.

This has been a little more than this Perl beginner can handle. Thanks for your help!

David


Rivotti
User

Mar 26, 2000, 4:11 PM

Post #2 of 4 (1300 views)
Re: separate files [In reply to] Can't Post

Hi David:

I've had so mutch help that I decided to help someone. I've been working with your code in the last hour and I think I've resolved your problem. Check it out:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


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

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


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

$number=1;

for ($start=3; $start<6; $start++){
open (SAVE, ">sample$number.txt");
for ($i=0; $i<=2; $i++){
foreach (@fileArr){
chomp;

($tmpStr = $_) =~ s/fruits/@fruits[$i]/g;

$tmpStr =~ s/7/$start/g;
print SAVE "$tmpStr\n";

}
}

close (SAVE);
$number++;

}
</pre><HR></BLOCKQUOTE>

This will create the files:

sample1.txt
sample2.txt
sample3.txt

with the text you want inside.


David4
Deleted

Mar 26, 2000, 9:41 PM

Post #3 of 4 (1300 views)
Re: separate files [In reply to] Can't Post

Hi Rivotti,

Thank you for your help!

I tested it out and works great. I understand what you did. It's an elegant solution. Good job!

Too bad I'm not in a position to help others yet.


David


Rivotti
User

Mar 27, 2000, 5:11 AM

Post #4 of 4 (1300 views)
Re: separate files [In reply to] Can't Post

Glad to help. See if you can resolve my problem posted as "Getting code from a section in a html page". Maybe you can help me.

Rivotti

 
 


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

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