print to file

J

johnd

Hello,

I need help to print static text to a file using a 'for' loop. My
program:

my textline1 = "This is text line one" ;
my textline2 = "This is text line two" ;
my textline3 = "This is text line three" ;

my $outfile = 'mytextfile.txt' ;

open(OUT, ">$outfile" ) or die "Cannot open for output: $!"

$count = 1 ;

while ($count <=3)
{
$printline = "$textline".$count ;
print OUT "$printline \n" ;
count ++ :
}

close OUT ;

I would expect the OUT file to contain the enrties:

This is text line one
This is text line two
This is text line three

But the OUT file contains the entries:

textline1
textline2
textline3


Please help me in understanding what I'm doing wrong.


Thank You!
 
B

Brian Wakem

johnd said:
Hello,

I need help to print static text to a file using a 'for' loop. My
program:

my textline1 = "This is text line one" ;
my textline2 = "This is text line two" ;
my textline3 = "This is text line three" ;


This is not real code.

my $outfile = 'mytextfile.txt' ;

open(OUT, ">$outfile" ) or die "Cannot open for output: $!"

$count = 1 ;

while ($count <=3)
{
$printline = "$textline".$count ;


$textline is undefined.


print OUT "$printline \n" ;


$printline now contains just the number in $count


count ++ :
}



That is not real code.


close OUT ;

I would expect the OUT file to contain the enrties:

This is text line one
This is text line two
This is text line three

But the OUT file contains the entries:

textline1
textline2
textline3



No your code doesn't compile so the file is empty.


Please help me in understanding what I'm doing wrong.


Use a hash to store the values.


#!/usr/bin/perl

use strict;

my %textline;
$textline{1} = "Thisistextlineone";
$textline{2} = "Thisistextlinetwo";
$textline{3} = "Thisistextlinethree";
my $outfile='mytextfile.txt';
open(OUT, ">$outfile" ) or die "Cannot open for output:$!";
foreach (1..3) {
print OUT "$textline{$_}\n";
}
close OUT ;
 
B

Bart Lateur

johnd said:
my textline1 = "This is text line one" ;
my textline2 = "This is text line two" ;
my textline3 = "This is text line three" ;

You forghot the "$" here.

Anyway, you shouldn't be trying to use symbolic references. You really
should be using an array.

my @textline = ("This is text line one",
"This is text line two",
"This is text line three");

for my $count = (0 .. 2) {
print OUT "$textline[$count]\n" ;
}
 
E

Eric Teuber

Bart said:
Anyway, you shouldn't be trying to use symbolic references. You really
should be using an array.

my @textline = ("This is text line one",
"This is text line two",
"This is text line three");

for my $count = (0 .. 2) {
print OUT "$textline[$count]\n" ;
}


The array would be the best way! But there is a little bug...

Use
foreach my $count = (0 .. $#textline) {
instead of
for my $count = (0 .. 2) {

So you also don't have to worry about the number of elements.

Eric
 
F

Fabian Pilkowski

* Eric Teuber said:
Bart said:
Anyway, you shouldn't be trying to use symbolic references. You really
should be using an array.

for my $count = (0 .. 2) {
print OUT "$textline[$count]\n" ;
}

The array would be the best way! But there is a little bug...

Yes, there is a little bug, but you're making the same mistake rather
than fixing it ;-)
Use
foreach my $count = (0 .. $#textline) {
^^^
This equal sign is the little bug. Delete it.
So you also don't have to worry about the number of elements.

Right, but this depends on what the OP really want. Perhaps he want to
print only the first three elements, even if there are more available.

regards,
fabian
 
E

Eric Teuber

Fabian said:
Yes, there is a little bug, but you're making the same mistake rather
than fixing it ;-)

Error Correction Support is working well ;-) at the news.

Eric
 
B

Bart Lateur

Fabian said:
for my $count = (0 .. 2) {
print OUT "$textline[$count]\n" ;
}

The array would be the best way! But there is a little bug...

Yes, there is a little bug, but you're making the same mistake rather
than fixing it ;-)

Yeah, sorry about that. That's what you get typing code directly in your
news client.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top