Re: Detect if third word in string contains a number
Re: Detect if third word in string contains a number
- Subject: Re: Detect if third word in string contains a number
- From: Nigel Garvey <email@hidden>
- Date: Thu, 10 May 2012 15:10:20 +0100
Axel Luttgens wrote on Wed, 09 May 2012 23:18:39 +0200:
>The first proposals, as of Shane or Michelle, were perfectly suitable.
>Unless... unless your service needs to perform the test (is the third
word
>a valid number?) many, many times per second and consume most of the
>service's computing duty, in which case optimizations such as the ones
>suggested by Nigel and Emmanuel may of course prove extremely useful.
Yes indeed. As I'd hoped the wink with my suggestion would convey,
although it _is_ slightly faster than attempting a coercion to number
and possibly recovering from an error as well, it wasn't meant to be
taken very seriously. Sorry if it's sidetracked Daniel from sorting out
the other problems in his script.
theDaniel wrote on Wed, 09 May 2012 21:42:37 -0400 :
>Here's where I'm stumped for now... _selection could contain the number
>in the first word (e.g. 3 John) or the second word (e.g. Luke 15) or the
>third word (e.g. II Kings 15:11)
>
>if _selection contains (number) then -- did the user select a reference
>to look up or a word to search for? If it has a number present then it
>is a reference
>
> (* if the selected text begins with the letter 'I' and includes a
>number in the string, then it is a reference to a book of the Bible such
>as 1, 2 or third john (not Isaiah). Change it to a '1', '2' or '3' *)
>
> -- here I used Nigel Garvey's script to get this number based on the
>number 36
> if word 1 of _selection is "I" and (word 3 of _selection is in
>"3635343323130292827262524222120191817161514110") then
> set newRef to switchText of (word 1 of _selection) from "I" to "1"
> set the clipboard to newRef
>
> else if word 1 of _selection is "II" and (word 3 of _selection is in
>"3635343323130292827262524222120191817161514110") then
> set newRef to switchText of (word 1 of _selection) from "II" to "2"
> set the clipboard to newRef
>
> else if word 1 of _selection is "III" and (word 3 of _selection is in
>"3635343323130292827262524222120191817161514110") then
> set newRef to switchText of (word 1 of _selection) from "III" to "3"
> set the clipboard to newRef
>
> else
> set the clipboard to _selection
> end if
A convenient structure for dealing with this part might be (going with
the digit string idea for the moment):
-- Identify which word's the number.
set numberWordNumber to 1
repeat with thisWord in _selection's words
if (thisWord is in "363534332313029282726252422120191817161514110") then exit repeat
set numberWordNumber to numberWordNumber + 1
end repeat
-- Act accordingly.
if (numberWordNumber is 1) then
-- The first word's the number.
else if (numberWordNumber is 2) then
-- The second word's the number.
else if (numberWordNumber is 3) then
-- The third word's the number.
set word1 to word 1 of _selection
if (word1 is in "I II III") then set the clipboard to ((count word1) as text) & space & text from word 2 to -1 of _selection
else --> numberWordNumber = 0 or numberWordNumber > 3.
error "None of the first three words of the selection is a number."
end if
If you need to do anything special when the first or second word is the
number, you can insert the relevant code in the appropriate place(s)
above.
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden