|
Posted by dakin999 on June 19, 2008, 6:14 am
Please log in for more thread options
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.
|
|
Posted by Jens Thoms Toerring on June 19, 2008, 8:25 am
Please log in for more thread options
> 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(/,/,$_);
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
Posted by MSwanberg on June 23, 2008, 3:16 pm
Please log in for more thread options > 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>) =A0#reading a line from file
> # Read the line into a set of variables
> =A0 =A0($1,$2,$3,$4)=3Dsplit(/,/,$_);
> ....
> ....
> # Buid an array with these varaibles
> =A0 =A0my @array =3D ([$1, $2, $3, $4]);
> =A0 =A0foreach my $r(@array) {
> =A0 =A0foreach (@$r){
>
> ... =A0 =A0 print <out> "$1\n";
> =A0 =A0 =A0 =A0print <out> "$2\n";
> =A0 =A0 =A0 =A0print <out> "$3\n";
> =A0 =A0 =A0 =A0print <out> "$4\n";
> =A0 =A0 =A0 =A0print <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
|
|
Posted by dummy on June 23, 2008, 6:34 pm
Please log in for more thread options wrote:
>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.
You're making it too complicated; only one loop necessary.
for each line: split on comma, join with new line and print:
use strict; use warnings;
print join("\n",split(/,/)),"\n" while (<DATA>);
__DATA__
yellow,blue,orange,red
black,white,red,pink
green,cerise,purple,terracotta
gray,turquoise,snow,navy
|
| Similar Threads | Posted | | printing with << | March 6, 2006, 4:20 pm |
| printing css | May 18, 2006, 7:16 pm |
| Printing "" and $iIN | August 4, 2006, 12:33 am |
| Printing on OSX | June 15, 2008, 4:21 pm |
| printing to web browser | October 21, 2004, 6:43 pm |
| Printing After A While Loop | January 24, 2005, 8:11 pm |
| printing PDF in win32 | January 31, 2005, 3:20 am |
| Not printing before ftp command | February 19, 2005, 2:07 pm |
| Formatted printing | June 14, 2005, 11:12 am |
| Exiting without printing | December 22, 2005, 8:48 pm |
|