• 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: Working with big lists
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Working with big lists


  • Subject: Re: Working with big lists
  • From: kai <email@hidden>
  • Date: Tue, 24 May 2005 01:18:39 +0100


On Sunday, May 22, 2005, at 07:58 pm, Rob Stott wrote:

I have a list in a text (.txt) file. I want to find out how many times each line occurs in the text file. For example, if the text file contained;

apple
apple
apple
orange
orange
pear

I want to be able to read it and get;

apple    3
orange    2
pear    1

It's not clear just how big a "big" list might be, and some of the suggested approaches don't seem to return accurate counts for me. Just for completeness, here's a vanilla version - which turns in a quite respectable performance here (tested on files of up to 20,000 lines):


-----------

on paras(t)
	try
		t's paragraphs
	on error number -2706 (* errOSAStackOverflow *)
		tell (count t's paragraphs) div 2 to ¬
			my paras(t's text 1 thru paragraph it) & ¬
			my paras(t's text from paragraph (it + 1) to -1)
	end try
end paras

on textItems(t)
	try
		t's text items
	on error number -2706 (* errOSAStackOverflow *)
		tell (count t's text items) div 2 to ¬
			my textItems(t's text 1 thru text item it) & ¬
			my textItems(t's text from text item (it + 1) to -1)
	end try
end textItems

to countOccurrences from f
	set d to text item delimiters
	set text item delimiters to return & return
	set t to return & paras(read f) & return
	set l to {}
	set c to count t's paragraphs
	repeat until c is 1
		set s to t's paragraph 2
		set text item delimiters to t's text 1 thru paragraph 3
		set t to textItems(t)
		set text item delimiters to ""
		set t to t as string
		tell (count t's paragraphs)
			set l's end to s & "    " & (c - it) div 2
			set c to it
		end tell
	end repeat
	set text item delimiters to return
	set l to l as string
	set text item delimiters to d
	l
end countOccurrences

countOccurrences from choose file

-----------

---
kai
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Working with big lists (From: Rob Stott <email@hidden>)

  • Prev by Date: Re: Setting a list to a random order
  • Next by Date: Metadata and folder actions questions
  • Previous by thread: Re: Working with big lists
  • Next by thread: Re: Waiting for script to exit
  • Index(es):
    • Date
    • Thread