Re: Working with big lists
Re: Working with big lists
- Subject: Re: Working with big lists
- From: Bastiaan Boertien <email@hidden>
- Date: Mon, 23 May 2005 13:56:43 +0200
Maybe this can halp you too
if thelist is like this in a text file
orange
orange
apple
orange
apple
mac
orange
etc
I tried it with a text file with 3000 records and it's still working fast
set theFile to (choose file) as string
try
open for access file theFile
set theBigList to read file theFile using delimiter {ASCII character 10, ASCII character 13}
close access file theFile
on error
close access file theFile
end try
set theResult to theBigList)
on theBigList)
script myScript
property theList : theBigList
property nameList : {}
property countList : {}
end script
repeat with x from 1 to count items in myScript's theList
if item x of myScript's theList is not in myScript's nameList then
set end of myScript's nameList to item x of myScript's theList
set end of myScript's countList to 1
else
repeat with Y from 1 to count items in myScript's nameList
if item x of myScript's theList is equal to item Y of myScript's nameList then
set item Y of myScript's countList to (item Y of myScript's countList) + 1
exit repeat
end if
end repeat
end if
end repeat
try
set theReturnText to item 1 of myScript's nameList & tab & tab & item 1 of myScript's countList
on error
set theReturnText to "File is empty"
end try
try
repeat with x from 2 to count myScript's nameList
set theReturnText to theReturnText & return & item x of myScript's nameList & tab & tab & item x of myScript's countList
end repeat
end try
return theReturnText
end CountTheItems
Op 23-mei-05 om 12:47 heeft Kim Hunter het volgende geschreven:
I made my list into a 60M list with 10 000 000 + lines and it took a total of 6 seconds
To do that it did
Cat file.txt >> file.txt
(for about 5 seconds)
On 23/5/05 10:51 AM, "Kim Hunter" <email@hidden> wrote:
This is a stand alone idea of another way you could do it:
set thelist to {"apple", "orange", "pear"} as list
set theFile to choose file
set itemcount to {"0", "0", "0"}
repeat with i from 1 to (number of items in thelist)
set theitem to item i in thelist
set item i of itemcount to do shell script "grep -c " & theitem & " " & (POSIX path of theFile)
end repeat
repeat with i from 1 to (number of items in thelist)
set displaylist to item i of thelist & " " & item i of itemcount
end repeat
display dialog item 1 of thelist & " " & item 1 of itemcount & "
" & item 2 of thelist & " " & item 2 of itemcount & "
" & item 3 of thelist & " " & item 3 of itemcount
On 23/5/05 4:58 AM, "Rob Stott" <email@hidden> wrote:
Hi all,
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
...at the moment, I'm doing this with the following script, and it works.
set theResult to ""
set theFile to choose file
set theContents to every paragraph of (do shell script "cat " & quoted form of (POSIX path of theFile))
--remove duplicates using LNS's List and Record OSAX
set theNewList to difference of {} and theContents with removing duplicates
repeat with theItem in theNewList
set theResult to theResult & theItem & "\t" & (do shell script "egrep -c " & quoted form of theItem & " " & quoted form of (POSIX path of theFile)) & "\r"
end repeat
...but some of the text files are REALLY long, and it takes quite a while. I was wondering whether anyone had any clever ideas for doing this a bit more quickly. Perhaps a big chunk of "do shell script" would speed things up, but it's still early days for me and shell scripting.
Any ideas or suggestions appreciated!
Thanks
Rob
_______________________________________________
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
_______________________________________________
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
_______________________________________________
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
_______________________________________________
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