problem with printing

problem with printing

am 19.06.2008 12:10:37 von 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 () #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 "$1\n";
print "$2\n";
print "$3\n";
print "$4\n";
print "\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.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem with printing

am 19.06.2008 19:40:59 von yitzle

Always:
use strict;
use warnings;


On Thu, Jun 19, 2008 at 6:10 AM, 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

__CODE__
open my $output, "> file" or die "Can't open file\n";
while (my $line = ) {
chomp $line;
my @array = split( ",", $line );
for my $item ( @array ) {
print $output "$item\n";
}
print $output "\n";
}
__DATA__
yellow,blue,orange,red
black,white,red,pink
__CODE__

__OUTPUT__
yellow
blue
orange
red

black
white
red
pink

__OUTPUT__

Does that help?

> while () #reading a line from file
> # Read the line into a set of variables
> ($1,$2,$3,$4)=split(/,/,$_);
$1, $2, ... are reserved variables used for RegEx's. I'd advise
against using them (or $a and $b - used in sort) as regular variables.

> # Buid an array with these varaibles
> my @array = ([$1, $2, $3, $4]);
The [] brackets are used to look up an element in an array. I'm not
sure what the intent or result here would be.

> foreach my $r(@array) {
> foreach (@$r){
Are you looping over the same data inside itself? Why the double loop?
$r is a scalar. Why are you casting it to an array and looping over
it?

> ... print "$1\n";
I hope you got something like:
print $out "$var\n";


HTH

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem with printing

am 19.06.2008 19:57:34 von romdav

On Jun 19, 5:10 am, akhils...@gmail.com (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 () #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 "$1\n";
> print "$2\n";
> print "$3\n";
> print "$4\n";
> print "\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 () {
($v1,$v2,$v3,$v4)=split(/,/,$_);
print "$v1\n";
print "$v2\n";
print "$v3\n";
print "$v4\n";
print "\n";
}


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem with printing

am 20.06.2008 02:51:35 von yitzle

On Thu, Jun 19, 2008 at 1:57 PM, romdav@gmail.com wrote:
> Do more simple and whit out numbers, some times the $1 .. are special
> vars
>
> while () {
> ($v1,$v2,$v3,$v4)=split(/,/,$_);
> print "$v1\n";
> print "$v2\n";
> print "$v3\n";
> print "$v4\n";
> print "\n";
> }

That drops anything after the fourth field. Unless this is desired,
you can use an array and have it deal with any amount of fields.
If your goal is simply to replace "," with "\n" via Perl (opposed to
e.g. sed) you can do something likeL

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem with printing

am 20.06.2008 04:09:31 von Rob Dixon

dakin999 wrote:
>
> 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

This is where you are getting confused. The data should be /either/ in separate
variables /or/ in an array. See below.

> 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

No. There is only one element in your array - it is a reference to an anonymous
array.

> it is printing 4 times each element and then moving to next
> element till it reaches eof().
>
> while () #reading a line from file
> # Read the line into a set of variables
> ($1,$2,$3,$4)=split(/,/,$_);

You have a missing open brace here. This code won't compile.

Never use $1, $2 etc. for you own purposes. Anything that doesn't start with a
letter (and even then a couple of those that do) are used by Perl without your
permission and may well be modified without you noticing.

For now, Let's say you meant

my ($aa, $bb, $cc, $dd) = split /,/;

> # Buid an array with these varaibles
> my @array = ([$1, $2, $3, $4]);

You've initialised @array to a single item. That item is [$aa, $bb, $cc, $dd],
which is an anonymous array. It's equivalent to

my @array;
$array[0] = [$aa, $bb, $cc, $dd];

> foreach my $r(@array) {

So this loop is executed one only, with $r equal to [$aa, $bb, $cc, $dd].

> foreach (@$r){

Now @$r is the same as the list ($aa, $bb, $cc, $dd), so within this loop $_
will be set to each of those values in turn. But you never use $_ within it so
the values in @$r are irrelevant - all that matters is that there are four of
them so you will do this four times.

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

And what you try to do is just print each of the four original variables for
each element of @$r, but this is incorrect syntax and again won't compile. What
you mean is

print out "xx\n";

If it was correct it would be the same as writing

foreach ('a' .. 'd') {
print out 1, "\n";
print out 2, "\n";
print out 3, "\n";
print out 4, "\n";
}

can you see that?



I suggest you meant to split into an array in the first place, and write
something like

while () { # read a line from file

# split the line into an array
my @data = split /,/;

foreach my $r (@data) {
print out "$r\n";
}
}



Please try to post real code that you are having problems with, or we could
spend a long time finding that all you have done is copied it wrongly into your
questions here.

Bed now .

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/