• 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: Counting Words and Characters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Counting Words and Characters


  • Subject: Re: Counting Words and Characters
  • From: "Marc K. Myers" <email@hidden>
  • Date: Fri, 13 Jul 2001 18:34:41 -0400
  • Organization: [very little]

> Date: Fri, 13 Jul 2001 12:15:35 -0500
> To: email@hidden, email@hidden
> From: Arthur Cormack <email@hidden>
> Subject: Counting Words and Characters
>
> --
> 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?

set lineToChange to "The quick brown fox jumped over the lazy dog."
changeLine(lineToChange, 4, "pig")

on changeLine(theLine, thePos, theRepl)
try
thePos as integer
on error
return 0
end try
set wordCnt to count words of theLine
if wordCnt < thePos then return ""
try
set theRepl to theRepl as text
on error
return 0
end try
if theRepl is "" then return 0
if thePos is 1 then
set part1 to ""
else
set part1 to (words 1 thru (thePos - 1) of theLine) as text
end if
if thePos is wordCnt then
set part2 to ""
else
set part2 to (words (thePos + 1) thru wordCnt of theLine) as text
end if
return part1 & theRepl & part2
end changeLine

--> "Thequickbrownpigjumpedoverthelazydog"

Marc K. Myers <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074

[7/13/01 6:34:06 PM]


  • Prev by Date: Re(2): Re(2): Re(2): Counting Words and Characters
  • Next by Date: FM insert Picture
  • Previous by thread: Re(2): Counting Words and Characters
  • Next by thread: Re: Re(2): Counting Words and Characters
  • Index(es):
    • Date
    • Thread