views:

150

answers:

1

Consider I have a .txt file as of the format,

Name: Abc

Id: 123

Age: 12

Address: xyz

I want to convert this file of the form,

Name    : Abc

Id      : 123

Age     : 12

Address : xyz

i.e the Colon after each tile should move 2 or 3 tab spaces. The title will be only a single word and wont have spaces. So the title wont be of the form(Your age:). So i can just read the first word and give tabs after that.

How can i do this? and will doing in perl be simpler or any other language.?

+2  A: 

Find the length of the biggest word, add n spaces to it. That's the padding to use when formatting each word.

PHP, for example, has a sprintf() function that you can look at. Various other languages provide formatted output as well.

In PHP, the following format should work - "%-<max>s : %s", where <max> is the length of the biggest word + some padding.

sprintf("%-25s : %s", $key, $value);

- makes the string left aligned.

Anurag
and if the max length is not known until runtime, then sprintf("%-*s : %s", $max_length, $key, $Value).
Wayne Conrad