[Tutor] printing columns
alan.gauld@bt.com
alan.gauld@bt.com
Fri, 26 Jul 2002 12:06:31 +0100
> code that produces a header file for a C++ program, but I've
> got a simple bit of polish I'd like to put on it.
> >>> menuItems = ['NEW', 'OPEN', 'CLOSE', 'SAVE', 'SAVEAS',
> ...
> 'CUSTOMTB', 'CASCADE', 'TILE', 'SPLIT']
> >>> myInt = 1
> >>> for item in menuItems:
> print '#define ROB_' + item + '\t\t' + str(myInt)
> myInt = myInt + 1
>
> Now I'm wondering how I could do this a little more neatly,
> organizing the output into two columns.
Doesn't your two tabs do exactly that? The other way of
course would be to create a format string with fixed
length fields...
The other issue of course is why you are using #defines
in a C++ program? Surely const would be better?
Or since you are assigning a series of integer values
use an enum which will do that automatically.
enum codes {NEW=1,
OPEN, // automatically assigned 2 etc...
...
TILE,
SPLIT};
Saves a mite of typing and you can now add new values
easily without worrying about maintaining the numeric
values etc. And its typesafe, nobody can kid the compiler
by passing a raw integer pretending to be a code to a
function which expects a code as input...
Altogether safer and more idiomatic C++.
Alan G.