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:
print out problem

 



David4
Deleted

Mar 9, 2000, 9:36 PM

Post #1 of 6 (847 views)
print out problem Can't Post

I'm having problem getting my expected output for this little program:

$prefer = 'testfile.txt';
@fruits = ('apples', 'oranges', 'peaches');
open(INFILE, $prefer) or die "Couldn't open the ". "testfile.txt file for writing.\n";

$start = 0;
while(<INFILE> ){
$Theline = $_;

while ($start < 3) {
($new = $Theline) =~ s/fruits/@fruits[$start]/g;
print $new;
$start++;}

}
close(INFILE);

The testfile.txt has:

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


My output was:

I like to eat apples. I think apples are delicious.
I like to eat oranges. I think oranges are delicicious.
I like to eat peaches. I think peaches are delicious.


I expected the output to be:

I like to eat apples. I think apples are delicious.
How many apples do you want?
I like this line to be printed too.

I like to eat oranges. I think oranges are delicious.
How many oranges do you want?
I like this line to be printed too.

I like to eat peaches. I think peaches are delicious.
How many peaches do you want?
I like this line to be printed too.

Thanks for your help!!


japhy
Enthusiast

Mar 10, 2000, 3:41 AM

Post #2 of 6 (847 views)
Re: print out problem [In reply to] Can't Post

The position of your loops is what's causing you problems. And, for one thing, $start never gets set back to 0 (so it's never less than 3 after the first line is played with).

Also, the ORDER of your loops is wrong for the logic you want. You're currently doing:

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


while (<FILE> ) {
for (0 .. 2) {
# change line
}
}
</pre><HR></BLOCKQUOTE>

... but the output you ask for means you want this order:

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


for (0 .. 2) {
while (<FILE> ) {
# change line
}
}
</pre><HR></BLOCKQUOTE>

Applying this, we fix your program:

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


# after setting vars and opening the file...
for $i (0,1,2) { # or: for $i (0 .. 2) {
while (<FILE> ) {
# the line is held in $_
s/fruit/$fruits[$i]/g; # operates on $_
print; # defaults to printing $_
}
}
</pre><HR></BLOCKQUOTE>


Cure
User

Mar 11, 2000, 8:13 AM

Post #3 of 6 (847 views)
Re: print out problem [In reply to] Can't Post

Hi David4

It doesnt work because you read to end of file in the first for loop. You would have to reset the pointer to the beg of the file.

This will work for you::

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

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

Cure


Cure
User

Mar 11, 2000, 8:42 AM

Post #4 of 6 (847 views)
Re: print out problem [In reply to] Can't Post

Hi David4

This will work for you also:

$prefer = 'testfile.txt';
@fruits = qw/apples oranges peaches/;
open(CURE, "$prefer") or die "Couldn't open the ". "testfile.txt file for writing.\n";

for $i(0,1,2) {
while (<CURE> ) {
chomp;
$_=~ s/fruits/$fruits[$i]/;
print "$_\n";
}
seek(CURE,0,0);
}
close CURE;


Cure

[This message has been edited by Cure (edited 03-11-2000).]


japhy
Enthusiast

Mar 11, 2000, 9:22 AM

Post #5 of 6 (847 views)
Re: print out problem [In reply to] Can't Post

Oh dear, I really thought that the <> operator reset the position in the file (did an automatic seek(FH,0,0) for you) after the file had been exhausted. But now I realize that would be kind of bad in certain cases.


David4
Deleted

Mar 11, 2000, 10:33 AM

Post #6 of 6 (847 views)
Re: print out problem [In reply to] Can't Post

$prefer = 'testfile.txt';
@fruits = ('apples', 'oranges', 'peaches');
open(INFILE, $prefer) or die "Couldn't open the ". "testfile.txt file for writing.\n";

for $i (0 .. 2) {

while (<INFILE> ) {
s/fruits/@fruits[$i]/g;
print;
}}

close(INFILE);


The output was:

I like to eat apples. I think apples are delicious. How many apples do you want? I like this line to be printed too.


I did not get my substitions for oranges and peaches. I've tried a few other ways and didn't get my expected output. Can you take another look at my program? Thanks!!!

 
 


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

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