• 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: limiting numeric runs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: limiting numeric runs


  • Subject: Re: limiting numeric runs
  • From: Paul Skinner <email@hidden>
  • Date: Thu, 9 Jan 2003 14:18:03 -0500

On Wednesday, January 8, 2003, at 07:36 PM, Gary Lists wrote:

On or about 1/8/03 12:47 PM, Paul Skinner wrote:

LimitNumericRuns("676798697675785", 4)
-->"6767"

on LimitNumericRuns(inputText, runLengthLimit)
set inputText to characters of inputText
set numeralRun to 0
repeat with i from 1 to length of inputText
set thisw to item i of inputText
if thisw is in {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"} then
set numeralRun to numeralRun + 1
if numeralRun > runLengthLimit then set item i of inputText to ""
else
set numeralRun to 0
end if
end repeat
set AppleScript's text item delimiters to ""
return inputText as string
end LimitNumericRuns

Paul,

From what you wrote, given the example you used, if that is all you want,
why not just return characters 1 thru runLengthLimit of inputText?

But, since that seems so very obvious, I presumed that a sample input might
also be: "abc1def2ghi3jkl4mno5pqr6stu7vwx8yz"

Indeed. I should have supplied real testing data with the question.

Here's the issue in a nutshell. I'm parsing plists and the precision for reals in plists are too large for applescript to handle. If you try to compile 0.83050847053527832 it will error with 'Syntax error. some object'. I need to be able to limit the precision. Truncating is fine for my purposes.
In fact, I find this precision to be useless since in this example case it is set by a slider in the GUI. A one pixel move of the slider results in a change of .1916229724884

So, here's real data sampled from a plist...

"<dict>
<key>Balance</key>
<real>0.071404993534088135</real>
<key>DeviceLevels</key>
<array>
<real>0.76713436841964722</real>
<real>0.82872772216796875</real>
</array>
<key>Level</key>
<real>0.83050847053527832</real>
</dict>
</dict>
</dict>
</dict>"

When I posted the question, I was trying to do the limiting after other parsing. Since then, I realized my folly and do the limiting first based on the tags for reals and so I no longer have to loop through irrelevant data; only the reals. I still use the same method I posted previously, but the speed over a large plist with no reals is immeasurably small and with a reasonable number of reals it is just measurably small! : )

here's the current version If anyone wants to critique it...

on LimitReals(inputText, runLengthLimit)
set AppleScript's text item delimiters to "<real>"
set inputText to text items of inputText
set numeralRun to 0
repeat with i from 2 to length of inputText
set AppleScript's text item delimiters to "</real>"
set thisWordList to text items of item i of inputText
set thisw to item 1 of thisWordList
if length of thisw > runLengthLimit then
set AppleScript's text item delimiters to ""
set thisw to characters of thisw
set numeralRun to 0
repeat with z from 1 to length of thisw
set thischar to item z of thisw
if thischar is in {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"} then
set numeralRun to numeralRun + 1
if numeralRun > runLengthLimit then set item z of thisw to ""
else
set numeralRun to 0
end if
end repeat
set thisw to thisw as string
end if
set item 1 of thisWordList to thisw
set AppleScript's text item delimiters to "</real>"
set thisWordList to thisWordList as string
set item i of inputText to thisWordList
end repeat
set AppleScript's text item delimiters to "<real>"
return inputText as string
end LimitReals

where you didn't just want the first X characters of a numeric string.
(Right?) but X number of numeric characters from a larger chunk of mixed
stuff...

So...

Your script chrono-timed with "0.033333335072"

The one below brought that time to "0.016666667536"

Nice! Unfortunately half the time is still HUGE when processing a large body of text.

Here is what I came up with...

snip speedier script

Gary

The current working version of the 'Plist Parser' script is available on my iDisk if you want to try it out. It quickly returns a record of the argument plist.

example with com.apple.soundpref.plist

set plistParser to load script alias "titan:Users:paulskin:Desktop:Plist Parser"
set plistReference to (choose file with prompt "Choose a plist file")
tell plistParser
return parsePlist(plistReference)
end tell
-->{
|AlertsUseMainDevice|:1,
|Devices|:{
|InputDevices|:{
|AppleDBDMAAudioDMAEngine:0|:{
|Balance|:0.0,
|DeviceLevels|:{
1,
1
},
|Level|:1
}
},
|OutputDevices|:{
|AppleDBDMAAudioDMAEngine:0|:{
|Balance|:0.071404993534,
|DeviceLevels|:{
0.841331720352,
0.905778825283
},
|Level|:0.906779646873
}
}
}
}
_______________________________________________
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.
  • Follow-Ups:
    • Re: limiting numeric runs
      • From: Donald Goyette <email@hidden>
    • Re: limiting numeric runs
      • From: Hans Haesler <email@hidden>
References: 
 >Re: limiting numeric runs (From: Gary Lists <email@hidden>)

  • Prev by Date: Re; Keynote, is it scriptable (XML?) -addendum
  • Next by Date: Re: Safari again
  • Previous by thread: Re: limiting numeric runs
  • Next by thread: Re: limiting numeric runs
  • Index(es):
    • Date
    • Thread