Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: strings pri...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 7 Topic 10989 of 11509
Post > Topic >>

Re: strings printing bug

by rob.dixon@[EMAIL PROTECTED] (Rob Dixon) Apr 26, 2008 at 02:55 PM

evan9021@[EMAIL PROTECTED]
 wrote:
> The following script is to read 4 consecutive lines at a time from a
> file, concatenate the first 3 lines
> (with a ", "), and print the result to STDOUT.  If the 3 lines aren't
> concatenated they print correctly, however
> if they are, the result is gibberish.  Any suggestions.   thx., EC.
>
--------------------------------------------------------------------------------------------------------------------------------------------------
> #!/bin/perl
> #  Read a series of 4 rows from a file and print the first 3 on
> #  the same line.
> 
> $file = 'example.txt';                # Name the file
> open(INFO, $file);                      # Open the file
> $row_num = 0;
> while (<INFO>) {
>     $i = $row_num%4;
>     if ($i <= 2) {
>         $col[$i] = "$_";
>     }
>     if ($i <= 1) {
>         chomp ($col[$i]);
>     }
>     if ($i == 2) {
>         #$row = join (', ', @[EMAIL PROTECTED]
);
>         printf ("%s", $col[0]);
>         printf (", ");
>         printf "%s, ", $col[1];
>         printf "%s\n", $col[2];
>     }
>     $row_num++;
> }
> close(INFO);                            # Close the file

Something like this is, I believe, easier both to code and to understand.

HTH,

Rob


use strict;
use warnings;

use constant FILE => 'example.txt';

open INFO, FILE or die $!;

my @[EMAIL PROTECTED]
 (<INFO>) {
  chomp;
  push @[EMAIL PROTECTED]
 $_;
  last if @[EMAIL PROTECTED]
 == 3;
}

print join(', ', @[EMAIL PROTECTED]
), "\n";
 




 7 Posts in Topic:
strings printing bug
evan9021@[EMAIL PROTECTED  2008-04-25 05:40:46 
Re: strings printing bug
krahnj@[EMAIL PROTECTED]   2008-04-25 19:40:26 
Re: strings printing bug
Uri Guttman <uri@[EMAI  2008-04-26 03:27:35 
Re: strings printing bug
rob.dixon@[EMAIL PROTECTE  2008-04-26 14:55:58 
Re: strings printing bug
evan9021@[EMAIL PROTECTED  2008-04-26 12:42:46 
Re: strings printing bug
rob.dixon@[EMAIL PROTECTE  2008-04-27 18:31:23 
Re: strings printing bug
chas.owens@[EMAIL PROTECT  2008-04-27 12:47:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 21:17:07 CDT 2008.