• 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: offset with spaces confusion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: offset with spaces confusion


  • Subject: Re: offset with spaces confusion
  • From: Arthur J Knapp <email@hidden>
  • Date: Fri, 03 May 2002 15:11:19 -0400

> Date: Fri, 3 May 2002 09:03:49 -0700
> Subject: Re: offset with spaces confusion
> From: Christopher Nebel <email@hidden>

> On Friday, May 3, 2002, at 08:36 AM, email@hidden wrote:
>
>> If the word "New" appears in the description, then I have to change the
>> colour of it. Simple, but some of the product styles include "Newark",
>> "Newport" and "Newton" to name a few. Which all contain "New" and
>> produce the wrong effect. Because the word "New" can appear anywhere, I
>> can't look for "<space>new<space>".


> In that case, try this:
>
> words of some_string contains "new"

Oh, I'm sorry, I didn't understand. He didn't want an actual offset/index,
just a more intelligent contains operation.


> words of "Newark" contains "new" --> false
> words of "New Ark" contains "new" --> true
>
> Added bonus: works with considering/ignoring case! Added slam: won't
> work with strings bigger than 32K.


OK, I quickly threw this together. It is not thoroughly test, (I suspect
that it's indexing is a little off), but I think you will get the general
idea:

on ContainsWord(str, wrd)
(*
* Note: wrd MUST BE what AppleScript considers a word.
*)

ignoring case --> just in case, (pun intended)

(* We need to protect against 2 AppleScript limits:
*
* Obtaining words fails when str's length approches 32k
* Obtaining words fails when word count approches approx. 4000
*)
set charLimit to 30000
set wordLimit to 4000

set charCount to count characters of str
set charX to 1
set charY to charLimit

repeat while (charY is less than or equal to charCount)

set str30k to str's text charX thru charY

set wordCount to count words in str30k
set wordX to 1
set wordY to wordLimit

repeat while (wordY is less than or equal to wordCount)

if (str30k's words wordX thru wordY contains wrd) then
return true
end if

set wordX to wordX + wordLimit
set wordY to wordY + wordLimit
end repeat
if (str30k's words wordX thru -1 contains wrd) then return true

set charX to charX + charLimit
set charY to charY + charLimit
end repeat

set str30k to str's text charX thru -1

set wordCount to count words in str30k
set wordX to 1
set wordY to wordLimit

repeat while (wordY is less than or equal to wordCount)

if (str30k's words wordX thru wordY contains wrd) then
return true
end if

set wordX to wordX + wordLimit
set wordY to wordY + wordLimit
end repeat

if (str30k's words wordX thru -1 contains wrd) then
return true
end if

end ignoring

end ContainsWord



{ Arthur J. Knapp, of <http://www.STELLARViSIONs.com>
<mailto:email@hidden>
try
<http://www.LateNightSW.com/>
on error number -128
end try
}
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: initialising loaded scripts
  • Next by Date: Re: More newbie queries: paths
  • Previous by thread: Re: offset with spaces confusion
  • Next by thread: Re: offset with spaces confusion
  • Index(es):
    • Date
    • Thread