Re: limiting numeric runs
Re: limiting numeric runs
- Subject: Re: limiting numeric runs
- From: "Arthur J. Knapp" <email@hidden>
- Date: Thu, 09 Jan 2003 10:28:27 -0500
>
Date: Wed, 8 Jan 2003 12:47:54 -0500
>
Subject: limiting numeric runs
>
From: Paul Skinner <email@hidden>
>
I need to limit the length of numeric runs in a given text. Does anyone
>
have a suggestion as to a non-looping solution?
>
This is too slow.
>
LimitNumericRuns("676798697675785", 4)
>
-->"6767"
>
on LimitNumericRuns(inputText, runLengthLimit)
[snip]
You only used numerals in your example input, but I think the handler
itself makes it clear that you want to process all numeric runs in any
given piece of text, so:
property kcAscii1 : ASCII character 1 --> osax call at compile-time
on BreakAtNumericRuns(s)
--> s == ie: "abc 31415 def 112233 ghi"
set o to AppleScript's text item delimiters --> save
set c to kcAscii1 --> sentinal character
repeat with i from 0 to 9
set AppleScript's text item delimiters to (i as string)
set s to s's text items
set AppleScript's text item delimiters to (c & i & c)
set s to s as string
-- ie: for i == 1 and for c == "^"
-- "abc 31415 def 112233 ghi" -->
-- "abc 3^1^4^1^5 def ^1^^1^2233 ghi"
end repeat
--> s == ie: "abc ^3^^1^^4^^1^^5^ def ^1^^1^^2^^2^^3^^3^ ghi"
set AppleScript's text item delimiters to (c & c)
set s to s's text items
set AppleScript's text item delimiters to ("")
set s to s as string
--> s == ie: "abc ^31415^ def ^112233^ ghi"
set AppleScript's text item delimiters to (c)
set s to s's text items
set AppleScript's text item delimiters to o --> restore
return s --> { "abc ", "31415", " def ", "112233", " ghi" }
end BreakAtNumericRuns
set str to "Hello 123456789 World 987654321"
set max to 4
set lst to BreakAtNumericRuns( str )
(* Even-items are the "numeric-runs" :
*)
repeat with i from 2 to lst's length by 2
if (lst's item i's length > max) then
set lst's item i to lst's item i's text 1 thru 4
end if
end repeat
set newStr to lst as string --> "Hello 1234 World 9876"
{ Arthur Knapp, of <
http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m
"Safe the Nature" - political graffiti in Prague
}
_______________________________________________
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.