Counting Words and Characters
Counting Words and Characters
- Subject: Counting Words and Characters
- From: Arthur Cormack <email@hidden>
- Date: Fri, 13 Jul 2001 12:15:35 -0500
--
Greetings and salutations appleScript wizards,
I have yet another newbie question.
I am writing a script that looks at log entries in a log file and
does a substitution of one word for another in each entry.
The handler looks like this:
---------------------------------------------
on changeLogEntryWord(lineToChange, wordNum, newWord)
--lineToChange is a line entry from a log file ... it is a
string that consists of multiple items
--this handler will change an item in this string and return
a new modified line
--wordNum is the position of the item in the log entry that
is to be changed
-- newItem is the new string that is going to be substituted
into this position
--the old item will be nuked
--it is important that newItem be a string with no spaces ...
so it must also be 1 item
--do an error check ... make sure that there is an item at
itemNum and that newItem is a continuous string
if lineToChange = "" then return 0 --need this
if wordNum = "" then return 0 --and this
if newWord = "" then return 0 --and this
if ((wordNum as number) > the number of items in
lineToChange) then return 0 -- can't change item that isn't there
set oldLine to lineToChange
set wordCount to the number of words in oldLine
if wordNum = 1 then
set beforeSnipSpacer to ""
set beforeSnip to ""
else
set beforeSnipSpacer to " "
set beforeSnip to characters 1 thru word (wordNum -
1) of oldLine
end if
if wordNum = wordCount then
set afterSnipSpacer to ""
set afterSnip to ""
else
set afterSnipSpacer to " "
set afterSnip to words (wordNum + 1) thru character
wordCount of oldLine
end if
set newString to ((beforeSnip & beforeSnipSpacer & newWord &
afterSnipSpacer & afterSnip) as string)
return newString
end changeLogEntryWord
---------------------------------------------
This handler "snips" the log entry in to two segments ... the segment
before the word that will be substituted, and the segment after it.
The problem that I am having is splicing the log entry back together.
The line of code that does the before segment "set beforeSnip to
characters 1 thru word (wordNum - 1) of oldLine" seems to work.
(initially it had read "set beforeSnip to words 1 thru word (wordNum
- 1) of oldLine", but this would remove the space characters (" "))
The problem, I believe is with the "afterSnip" ... I need a line of
code that will get all of the characters after "wordNum"(the integer
that indicates which word in the log entry) thru to the last
character of the last word. I am stumped.
I think that the problem with the line: " set afterSnip to words
(wordNum + 1) thru character wordCount of oldLine"
is that words are mentioned bofore characters ... so it is counting
words not characters ...
What I want it to do is something like this: set afterSnip to
characters (the first character of the first word after wordNum) thru
(the last character of the last word of oldLine).
Any suggestions?
Regards, and Thanks
arthur cormack
_________________________
producer, interactive
the online group, TVOntario
W:416.484.2600.2010 C: 416.453.4369
E: email@hidden