• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Line count of a referenced text file?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Line count of a referenced text file?


  • Subject: Re: Line count of a referenced text file?
  • From: Christopher Nebel <email@hidden>
  • Date: Tue, 13 Nov 2001 09:46:36 -0800

On Monday, November 12, 2001, at 01:49 PM, Matthew Broms wrote:

Using purely AppleScript, can I get the line count of a text file using just
a reference to it (NOT reading the contents into a variable)? If I can, can
I get the count of any delimiter of my choosing? When I try to do a "count
paragraphs", I get an error.

Just to be clear, someone somewhere is going to have to read the entire file to find out how many paragraphs there are. The file system doesn't store that kind of information like it does the file length, so the only way to find it out is to inspect the file. Of course, you don't have to have the entire file in memory all at once.

The following will work, and only holds a single line in memory at once:

set line_count to 0
set fp to open for access the_file
repeat
try
read fp until return
set line_count to line_count + 1
on error
-- probably ran out of lines to read.
exit repeat
end
end
close access fp

The parameter to "until" -- return in this case -- can be any single-byte character. (Allowing longer delimiters is already an enhancement request.)

Alternatively, you could read the file in fixed-size chunks and then count the number of delimiter characters in each chunk. (Again, this only works if your delimiter is one byte -- otherwise, it could be split between chunks, and recognizing that case makes things much harder.)

(same as above, but replace the try body with this:)
read fp for 4096
set line_count to line_count + count_characters(the result, return)

You can set the chunk size to anything you want; in general, the higher you set it, the faster the script will run, but the more memory it will need. How you implement count_characters is up to you; here's one solution:

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

As Paul Berkowitz pointed out, if you know you're counting lines, it's faster to just say "count paragraphs of s".


--Chris Nebel
AppleScript Engineering

P.S.: To be fair to AppleScript, every language that I've ever heard of requires you to do it like this: you have to open the file and read it yourself. It's not an unusual lack.


  • Follow-Ups:
    • Re: Line count of a referenced text file?
      • From: Matthew Broms <email@hidden>
References: 
 >Line count of a referenced text file? (From: Matthew Broms <email@hidden>)

  • Prev by Date: Re: OS X to OS 9 restart script...
  • Next by Date: Fast Loading & updating of Handler Libraries
  • Previous by thread: Re: Line count of a referenced text file?
  • Next by thread: Re: Line count of a referenced text file?
  • Index(es):
    • Date
    • Thread