Printing Problems

D

dakin999

Hi,

I have following code which works ok. It does following:

1. reads data from a input file
2. puts the data into seperate variables in a array
3. reads from this array and prints out to another file

It works except that it prints the same record 4 times. I can see I
have missed some thing in my array definition as their are 4 elements
in array, it is printing 4 times each element and then moving to next
element till it reaches eof().


while (<input>) #reading a line from file
# Read the line into a set of variables
($1,$2,$3,$4)=split(/,/,$_);
.....
.....
# Buid an array with these varaibles
my @array = ([$1, $2, $3, $4]);
foreach my $r(@array) {
foreach (@$r){

.... print <out> "$1\n";
print <out> "$2\n";
print <out> "$3\n";
print <out> "$4\n";
print <out> "\n";


The out put is coming like this:

yellow
blue
orange
red

yellow
blue
orange
red

yellow
blue
orange
red

yellow
blue
orange
red

black
white
red
pink

black
white
red
pink

black
white
red
pink

black
white
red
pink

Clearly it should just print one time and go to the next record....

Please suggest.
 
J

Jens Thoms Toerring

dakin999 said:
I have following code which works ok. It does following:
1. reads data from a input file
2. puts the data into seperate variables in a array
3. reads from this array and prints out to another file
It works except that it prints the same record 4 times. I can see I
have missed some thing in my array definition as their are 4 elements
in array, it is printing 4 times each element and then moving to next
element till it reaches eof().
while (<input>) #reading a line from file
# Read the line into a set of variables
($1,$2,$3,$4)=split(/,/,$_);

This can't be your real program since $1, $2 etc. are read-only
variables. That makes it difficult to guess what you're really
doing...
....
....
# Buid an array with these varaibles
my @array = ([$1, $2, $3, $4]);
foreach my $r(@array) {
foreach (@$r){
... print <out> "$1\n";
print <out> "$2\n";
print <out> "$3\n";
print <out> "$4\n";
print <out> "\n";

What do you expect $1, $2 etc. to be set to here? And the '<>'
around 'out' also can't be right. Please post your real code,
not something you just asssume to have some resemblance to
your code.

Why aren't you simply doing something like

use strict;
use warnings;

my ( $input, $out, @arr ) = ( *STDIN, *STDOUT );

push @arr, [ split /,/, $_ ] while <$input>;

for my $r ( @arr ) {
print $out "$_\n" for @$r;
print $out "\n";
}

or, even simpler, if you don't want to safe the data you read from
the input in an array:

use strict;
use warnings;

my ( $input, $out ) = ( *STDIN, *STDOUT );

while ( my $line = <$input> ) {
print $out "$_\n" for split /,/, $line;
print $out "\n";
}
Regards, Jens
 
M

MSwanberg

Hi,

I have following code which works ok. It does following:

1. reads data from a input file
2. puts the data into seperate variables in a array
3. reads from this array and prints out to another file

It works except that it prints the same record 4 times. I can see I
have missed some thing in my array definition as their are 4 elements
in array, it is printing 4 times each element and then moving to next
element till it reaches eof().

while (<input>)  #reading a line from file
# Read the line into a set of variables
   ($1,$2,$3,$4)=split(/,/,$_);
....
....
# Buid an array with these varaibles
   my @array = ([$1, $2, $3, $4]);
   foreach my $r(@array) {
   foreach (@$r){

...     print <out> "$1\n";
       print <out> "$2\n";
       print <out> "$3\n";
       print <out> "$4\n";
       print <out> "\n";

The out put is coming like this:

yellow
blue
orange
red

yellow
blue
orange
red

yellow
blue
orange
red

yellow
blue
orange
red

black
white
red
pink

black
white
red
pink

black
white
red
pink

black
white
red
pink

Clearly it should just print one time and go to the next record....

Please suggest.


Try simply:

print out "$var\n";

Having the brackets around out, such as

print <out>...

isn't the right way to print to the "out" filehandle.

As well, why do you have 2 loops? And why are you putting the
elements into an anonymous array, and then having that as the sole
element of the @array array? Couldn't you just simplify this entire
program as follows:

while (<input>) {
print join("\n",split(/,/,$_),"\n\n";
}

or even:

while (<input>) {
s/,/\n/g;
print "$_\n\n";
}

-Mike
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top