1

As I asked in my previous question(Link) about concatenating a multipart string of variable lengths, I used the method answered there by rkhayrov and now, my function looks like this:

local sToReturn = string.format( "\t%03s\t%-25s\t%-7s\n\t", "S. No.", "UserName", "Score" )
SQLQuery = assert( Conn:execute( string.format( [[SELECT username, totalcount FROM chatstat ORDER BY totalcount DESC LIMIT %d]], iLimit ) ) )
DataArray = SQLQuery:fetch ({}, "a")
i = 1
while DataArray do
  sTemp = string.format( "%03s\t%025s\t%-7d", tostring(i), DataArray.username, DataArray.totalcount )
  sToReturn = sToReturn..sTemp.."\n\t"
  DataArray = SQLQuery:fetch ({}, "a")
  i = i + 1
end

But, even now, the value of score is still not following the order as required. The max length of username is 25. I've used %025s inside the while loop because I want the usernames to be right-justified, while the %-25s is to make the word UserName centre justified.

EDIT

Current output:

Current output!

Required Output:

Displaying the list of top 5 chit-chatters.
S. No.      UserName                Score  
  1                     Keeda   9440   
  2                    _2.2_™   7675   
  3                       aim   7057   
  4                  KGBRULES   6770   
  5                     Guddu   6322   

I think it's because of difference in fonts, but since most of the clients have Windows 7 default fonts(Tahoma/Verdana at 11px), I need optimum result for at-least that.

Cœur
  • 37,241
  • 25
  • 195
  • 267
hjpotter92
  • 78,589
  • 36
  • 144
  • 183

3 Answers3

5

I think it's because of difference in fonts

It is. string.format formats by inserting whitespace. That only works for a fixed width fonts (i.e. all characters have the same width, including whitespace).

since most of the clients have Windows 7 default fonts(Tahoma/Verdana at 11px)

In what? How are they viewing your output? Do you write it to a textfile, that they then open in the editor of their choice (likely Notepad)? Then this approach will simply not work.

Don't know enough about your output requirements to steer you any futher, but it's worth noting that everyone has a browser so HTML output is very portable.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • So, if I remove the `\t`s from the `string.format` function, and replace them with `string.rep(" ", 8)`, would that make my display common on all kinds of fonts/OS? – hjpotter92 Apr 16 '12 at 10:58
  • The output comes as a plain text on a P2P client.(such as DC++) – hjpotter92 Apr 16 '12 at 11:00
  • If you have to output in plain text -- no markup available to you, no means of creating tables or positioning text, and you have no control over the output font -- then you're boned. You have no means for achieving horizontal alignment. – Mud Apr 16 '12 at 16:32
  • hmm, then I'll just go along with the current formatting and let the users think: **[It's a feature, not a bug.](http://www.codinghorror.com/blog/2008/11/thats-not-a-bug-its-a-feature-request.html)** :P – hjpotter92 Apr 16 '12 at 17:30
  • Why on Earth is a plain-text-only program not using a fixed-width font? Can that be configured on the client? – Mark Reed Apr 16 '12 at 23:48
1

string.format doesn't truncate - the width of the field is minimum, not maximum. You'll have to truncate the strings to 25 characters yourself with something like DataArray.username:sub(0,25).

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
1

I'd remove the tabs from the string.format; and use the justification provided by %25s only. Won't be perfect but will probably be closer.

Use a fixed-width font if you can.

daurnimator
  • 4,091
  • 18
  • 34