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 3 of 7 Topic 10989 of 11401
Post > Topic >>

Re: strings printing bug

by Uri Guttman <uri@[EMAIL PROTECTED] > Apr 26, 2008 at 03:27 AM

>>>>> "e" == evan9021  <evan9021@[EMAIL PROTECTED]
> writes:

  e> The following script is to read 4 consecutive lines at a time from a
  e> file, concatenate the first 3 lines
  e> (with a ", "), and print the result to STDOUT.  If the 3 lines aren't
  e> concatenated they print correctly, however
  e> if they are, the result is gibberish.  Any suggestions.   thx., EC.
  e>
--------------------------------------------------------------------------------------------------------------------------------------------------
  e> #!/bin/perl
  e> #  Read a series of 4 rows from a file and print the first 3 on
  e> #  the same line.

  e> $file = 'example.txt';                # Name the file
  e> open(INFO, $file);                      # Open the file
  e> $row_num = 0;

you forgot to comment on that line. maybe # assign 0 to $row_num?

  e> while (<INFO>) {
  e>     $i = $row_num%4;

you don't need a row num. $. will do that for you. perldoc perlvar.

  e>     if ($i <= 2) {
  e>         $col[$i] = "$_";

useless use of quotes. 
  e>     }
  e>     if ($i <= 1) {
  e>         chomp ($col[$i]);
  e>     }
  e>     if ($i == 2) {
  e>         #$row = join (', ', @[EMAIL PROTECTED]
);
  e>         printf ("%s", $col[0]);

why use printf there? print $col[0] does the SAME thing. this is not c
and printf is rarely used (especially when compared to print).

  e>         printf (", ");
  e>         printf "%s, ", $col[1];
  e>         printf "%s\n", $col[2];

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

  e>     }
  e>     $row_num++;
  e> }
  e> close(INFO);                            # Close the file

another informative comment. who taught you that style? seriously, why
do your comments say exactly what the code says? a useful rule is code
is what and comments are why.

uri

-- 
Uri Guttman  ------  uri@[EMAIL PROTECTED]
  --------  http://www.sysarch.com
--
-----  Perl Code Review , Architecture, Development, Training, Sup****t
------
--------- Free Perl Training --- http://perlhunter.com/college.html
---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com
---------
 




 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 Sun Jul 6 6:59:48 CDT 2008.