find

Tariel Gogoberidze PMDA at earthlink.net
Tue Apr 23 12:11:02 EDT 2002


> Message: 16
> Date: Tue, 23 Apr 2002 08:00:10 +0200
> From: yves COPPE <yvescoppe at skynet.be>
> Subject: find
> 
> Hi,
> 
> I'd like a script which search for all occurences of a word in a list
> and returns the number of all lines.
> 
> an example :
> 
> a list with :
> 
> Peter
> John
> Richard
> John
> George
> John
> Philip
> Tom
> 
> So if i call the code for "John", the result should be :
> 
> 2
> 4
> 6
> --
> Greetings.
> 
> Yves COPPE

This script is from MetaCard example stack.It works not only on the list
but  on any text file and it is very fast.

This  example  demonstrate the use of associative arrays. The script
parses a text file, count the occurance of each word, and display the
result in a field
on mouseUp
  put empty into field "result"
  answer file "Select a text file for input:"
  if it is empty then exit mouseUp
# let user know we're working on it
  set the cursor to watch
  put it into inputFile
  open file inputFile for read
  read from file inputFile until eof
  put it into fileContent
  close file inputFile
# wordCount is an associative array, its indexes are words
# with the contents of each element being number of times
# that word appears
  repeat for each word w in fileContent
    add 1 to wordCount[w]
  end repeat
# copy all the indexes that is in the wordCount associative array
  put keys(wordCount) into keyWords
# sort the indexes -- keyWords contains a list of elements in array
  sort keyWords
  repeat for each line l in keyWords
    put l & tab & wordCount[l] & return after displayResult
  end repeat
  put displayResult into field "result"
end mouseUp

Best regards
Tariel Gogoberidze



More information about the use-livecode mailing list