re: counting lines of a file
re: counting lines of a file
- Subject: re: counting lines of a file
- From: Timothy Bates <email@hidden>
- Date: Fri, 16 Nov 2001 10:25:07 +1100
Just FYI, this:
to count_characters(s, c)
set {tid, AppleScript's text item delimiters} to {text item delimiters,
c}
set n to (count (text items of s)) - 1
set AppleScript's text item delimiters to tid
return n
end count_characters
returns 1 less than the number of lines. One may or may not wish to trap the
returns which many unix editors force at the end of a file (and not return
them as lines).
Maybe something like
to count_characters(s, c)
set {tid, AppleScript's text item delimiters} to {text item delimiters,
c}
set n to (count (text items of s)) - 1
if item (n + 1) of text items of s is "" then set n to n - 1
set AppleScript's text item delimiters to tid
return n + 1
end count_characters
More importantly neither the 3read lines2 nor the 3read chunks and count
lines2 strategies for getting the number of lines in a file work with unix
line endings. I guess this is tricky, as \n and \r are both valid
within-line characters in the other OS. The way that pepper (the best editor
on a mac or anywhere else tm) gets around this is to store the line ending
and character type (macos or unicode etc) as meta data. Then you can use
3\n2 as a universal line ending identifier in search strings.
I suppose you could read a chunk, search for either \n or \r, then use which
ever one is found in the largest quantity as the line ending and proceed
from there.
Tim