On Jun 19, 5:10 am, [EMAIL PROTECTED] (Dakin999) 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.

Do more simple and whit out numbers, some times the $1 .. are special
vars


while (<input>) {
  ($v1,$v2,$v3,$v4)=split(/,/,$_);
  print <out> "$v1\n";
  print <out> "$v2\n";
  print <out> "$v3\n";
  print <out> "$v4\n";
  print <out> "\n";
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to