Printing Problems

Discussion in 'Perl Misc' started by dakin999, Jun 19, 2008.

  1. dakin999

    dakin999 Guest

    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.
     
    dakin999, Jun 19, 2008
    #1
    1. Advertisements

  2. 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...
    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, Jun 19, 2008
    #2
    1. Advertisements

  3. dakin999

    MSwanberg Guest


    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
     
    MSwanberg, Jun 23, 2008
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.