Count number of commas in line of text

This seems simple, but I’m a novice. I need to count the number of commas in each line of text in a text file. If there are 17 commas in the first line, I need to go to the next line and continue the script. I’ve tried

Tell application “BBEdit”
open file XXXXXXX
select line 1
If count {“,”} = 17 then
select line 2
select line 1
end if
endt tell

The count always returns 1. I know there are 17. I’m at a loss.

Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8) Gecko/20051107 Camino/1.0b1
Operating System: Mac OS X (10.4)

This will be slow if the document is large (it’s late) but it will find the character “,”:

tell application "BBEdit"
	set C to 0
	set theLine to contents of line 1 of window 1 -- doc already open
	set char to characters of theLine
	repeat with aChar in char
		if contents of aChar = "," then set C to C + 1
	end repeat
end tell

It’s a start.

If it’s a plain text file, this could do the job:

(count (read alias "path:to:file.txt" using delimiter ",")) - 1

This may fail for large files or files containing thousands of commas.

Similar to jj:

on countChr(t, c)
	set {tid, my text item delimiters} to {my text item delimiters, c}
		set chrCount to (count of every text item of t) - 1
		set my text item delimiters to tid
		return chrCount
	on error
		set my text item delimiters to tid
		return 0
	end try
end countChr

You call the handler as in the following:

set theString to "This:Fake:Path:"
set chrCount to countChr(theString, ":")
--> 3


A small scripting addition I just hacked together, tested it on my iBook (MacOS 10.3.9). Unzip the file and put the OSAX into your ScriptingAdditions dir, then you can use it like this:

countchar "," in "1,2,3,4"

It returns the number of occurences.

Best regards,

EDIT: Whoopzie… just fixed a small bug which caused the command to return 1 less then the actual number of occurences…

Model: iBook g3/800 14"
Browser: Safari 312.3.3
Operating System: Mac OS X (10.3.9)

Hi there, Daniel!

Are you now in the obj-c business? :slight_smile:

Seems there is a bug in “countchar”, though, as it returns allways 2049 in texts with more than 2047 occurrences… (eg, for 2048 occurrences, it will return 2049; and the same number for 2049, 2050, 2051…).

Hey JJ,

I’ve learned some ObjC, but this one is just some plain ol’ C :wink: However, you are right about the limit/bug. The reason is that I used a fixed-length buffer and the OSAX doesn’t accept strings longer than 2048 - but doesn’t give you an error if you exceed that limit (As I said, it was just “hacked together” - no serious development involved here).

Anyway, I fixed that, any string longer than 2048 chars will now give you an error:

This is what I used to test it:

on run
end run

on countchars(j)
	set i to 0
	set testtext to ""
	repeat until i = j
		set testtext to (testtext & ",") as text
		set i to i + 1
	end repeat
	set charcount to (countchar "," in testtext)
	display dialog "The testtext contains " & charcount & " commas"
end countchars

(EDIT: fixed d/l link)

Model: iBook g3/800 14"
Browser: Safari 312.3.3
Operating System: Mac OS X (10.3.9)