In formatted printing, you can often use a leading "+" in the format string to force a positive sign to be added in front of positive numbers - for example "%+4d" means an integer, to base 10, 4 character positions as a minimum, right justified.
Here's an example of that, in code, in Lua
for john = -10, 10, 1 do
print (string.format("xx %+4d yy",john))
end
And here is how it runs:
[trainee@easterton o8]$ lua sf
xx -10 yy
xx -9 yy
xx -8 yy
xx -7 yy
xx -6 yy
xx -5 yy
xx -4 yy
xx -3 yy
xx -2 yy
xx -1 yy
xx +0 yy
xx +1 yy
xx +2 yy
xx +3 yy
xx +4 yy
xx +5 yy
xx +6 yy
xx +7 yy
xx +8 yy
xx +9 yy
xx +10 yy
[trainee@easterton o8]$
The same technique applies in Perl, and in other languages too.
Other leading characters:
0 - Leading zero fill
- - Left justify
(written 2008-10-15)
Associated topics are indexed under
P207 - Perl - File Handling [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
[3830] Traversing a directory in Perl - (2012-08-08)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
[2818] File open and read in Perl - modernisation - (2010-06-19)
[2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
[2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
[1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1709] There is more that one way - Perl - (2008-07-14)
[1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[1416] Good, steady, simple example - Perl file handling - (2007-10-30)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[867] Being sure to be positive in Perl - (2006-09-15)
[702] Iterators - expressions tha change each time you call them - (2006-04-27)
[618] Perl - its up to YOU to check your file opened - (2006-02-23)
[616] printf - a flawed but useful function - (2006-02-22)
[255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
[114] Relative or absolute milkman - (2004-11-10)
[12] How many people in a room? - (2004-08-12)
U110 - Lua - Input / Output [3558] Python or Lua - which should I use / learn? - (2011-12-21)
[2344] Reading a data file and sensing EOF in Lua - (2009-08-10)
[1744] Lua examples, Lua Courses - (2008-08-08)
5397
Some other Articles
Passing a table from Lua into CCalling functions in C from your Lua script - a first HowToHow many cups of coffee?Lua Course, and the Wiltshire Countryside tooFormatting with a leading + / Lua and PerlValidating Credit Card NumbersJob applicationOxford in PicturesAlfred the GreatNext in the sequence - courses next year (2009)