• 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 12:40:49 -0400

>>> ... So why don't I get the answer I expect with
>>> offset:

>>> ignoring white space
>>> offset of "c" in "a b c d"
>>> -->8

>> I'm not sure what answer you want. 4?

> Er, no. 3. 'c' is the third letter ignoring white space.

Right, sorry, I meant 3.

>>> I assume this means offset ignores the ignoring statement. Is there a
>>> logical reason or is this a bug?

>> While I'm not sure about the usefulness of finding an offset while
>> ignoring whitespace

> It cropped up this morning. I am working on a script that creates price
> lists. In one bit of fiddling, 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>" so I was
> looking in to other ways of doing it. That's when I noticed this.


The following is a bit buggy, because of a number of AppleScript limits
regarding words and list-creation, but for relatively small strings, it
should be pretty fast:


on OffsetOfWord(str, sub)
(*
* NOTE: sub MUST BE what AppleScript considers a word.
*)

if (str's words contains sub) then --> breaks at approx. 4040 words

(* Binary "contains" search:
*)
set l to 1
set r to count words in str
set m to 1

repeat until str's word m = sub

set m to (l + r) div 2

if (str's words l thru m contains sub) then

set r to m
else
set l to m + 1

end if
end repeat

return ((str's text 1 thru word m)'s length) - (sub's length) + 1
else
return 0
end if
end OffsetOfWord

set searchString to "Newport, Newspaper, A Brand New Car!!!"
set findWord to "new"

OffsetOfWord(searchString, findWord) --> 29



{ Arthur J. Knapp, of <http://www.STELLARViSIONs.com>
<mailto:email@hidden>
try
<http://maccentral.macworld.com/columns/briggs.shtml>
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: applescript-users digest, Vol 3 #421 - 13 msgs
  • 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