views:

55

answers:

2

According to TkDocs:

The "1.0" here represents where to insert the text, and can be read as "line 1, character 0". This refers to the first character of the first line; for historical conventions related to how programmers normally refer to lines and characters, line numbers are 1-based, and character numbers are 0-based.

I hadn't heard of this convention before, and I can't find anything relevant on Google. Can anyone explain this to me please?

+2  A: 

It really is nothing more than convention, but here is a suggestion.

Character positions are generally thought of in the same way as a Java iterator, which is a "pointer" to a position between two characters. Thus the first character is the one after index position 0. Substrings are taken between two inter-character positions, for instance.

Line positions on the other hand are generally thought of more in the way of a .NET enumerator, which is a "pointer" to the item itself, not to a position in between. Thus the first line is the line at position 1.

David M
+2  A: 

I think you're referring to Tk's text widget. The man page says:

Lines are numbered from 1 for consistency with other UNIX programs that use this numbering scheme.

Although, I'm not sure which Unix tools it's talking about.

Update: As mentioned in the comments, it looks like a lot of unix text manipulation tool starts line numbering at 1. And tcl/tk having a unix origin, it makes sense to be as compatible as possible with the underlying OS environment.

slebetman
`nl` uses 1-based line numbering: http://en.wikipedia.org/wiki/Nl_(Unix)
Skilldrick
For example, awk, ed, nl, sed, all number lines like this.
P-Nuts
Now I remember, `grep -n` also numbers lines starting from 1.
slebetman