in reply to Re^4: Writing to a file using Formats
in thread Writing to a file using Formats
One thing that I strongly suggest is "incremental development". Perl compiles and runs so quickly that it is possible to write some code and test it, then write more code. For example, to test the command loop, this is it:
I'm not saying to try a whole bunch of random stuff in the hope that it will work, but rather take a small piece, think about the errors, make corrections, get that piece working then get the next piece working.#!usr/bin/perl -w use strict; while ( (print "Enter a pair of numbers (space between) or quit: "), (my $line =<STDIN>) !~ /^\s*q(uit)?\s*$/i ) { print $line; }
Printing:
I you are having trouble with a printf(), just use the standard Perl print,
print "$var1 $var2\n"; to make sure that the variables are defined, etc.
Use of "warnings" will show at runtime if a $var is undefined in the print.
One common error in printf format statements is not leaving a space after each format descriptor if you want tabular output. The WIDTH is a minimum width, and if what is required is more, it will "spill over". Putting a space between format fields guarantees on whitespace character between columns which is usually what you want.
#!usr/bin/perl -w use strict; my $string_a="ABCDEGFH"; my $string_b="X"; printf("%-5s%s\n", $string_a, $string_b); #$string_a is too long printf("%-5s %s\n", $string_a, $string_b); #space between fields printf("%-5s%s\n", $string_b, $string_a); #looks ok, but not! #If $string_b>=5 chars! #no space between columns. __END__ prints: ABCDEGFHX ABCDEGFH X X ABCDEGFH
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Writing to a file using Formats
by biohisham (Priest) on Aug 05, 2009 at 11:26 UTC |