Re: cutting multiple sections from a string?
Re: cutting multiple sections from a string?
- Subject: Re: cutting multiple sections from a string?
- From: Takaaki Naganoya <email@hidden>
- Date: Thu, 05 Sep 2013 15:05:51 +0900
Your sorting program returned wrong result.
{"513", "212", "713", "161", "240", "932", "193"} --> {"932", "713", "513", "212", "240", "161", "193"}
So, I recommend not to use it.
If you want high-speed and right result, this sorting routine process 10,000 item list in one second and returns right result.
<AppleScript>
on shellSortDescending(aSortList)
script oBj
property list : aSortList
end script
set len to count oBj's list's items
set gap to 1
repeat while (gap ≤ len)
set gap to ((gap * 3) + 1)
end repeat
repeat while (gap > 0)
set gap to (gap div 3)
if (gap < len) then
repeat with i from gap to (len - 1)
set temp to oBj's list's item (i + 1)
set j to i
repeat while ((j ≥ gap) and (oBj's list's item (j - gap + 1) < temp))
set oBj's list's item (j + 1) to oBj's list's item (j - gap + 1)
set j to j - gap
end repeat
set oBj's list's item (j + 1) to temp
end repeat
end if
end repeat
return oBj's list
end shellSortDescending
</AppleScript>
Here is AppleScript sorting-routine museum.
http://piyocast.com/as/archives/category/sorting/
--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/
On 2013/09/05, at 12:56, Alex Hall <email@hidden> wrote:
> My sorting method is for sorting the entire list (thankfully I only have to sort a few items, never ten thousand, or even a hundred). Unless I missed something, your function seems to only grab the biggest list item. You'd have to call it on every list once you remove and store that biggest item, so, in your case, that's ten thousand calls, each processing 10000-(number of runs) items. Again, I may have missed a key here, since you use a script object and I've never done much with that.
> On Sep 4, 2013, at 11:21 PM, Takaaki Naganoya <email@hidden> wrote:
>
>> Your "insertionSortTupleList" is too slow.
>> It took 14 seconds to process 10000 item list on my MacBook Pro Retina.
>> This one took within 1 second. Use this to pick up maximum item.
>>
>> <AppleScript>
>> on maximumFromList(nList)
>> script o
>> property nl : nList
>> end script
>>
>> set max to item 1 of o's nl
>> repeat with i from 2 to (count nList)
>> set n to item i of o's nl
>> if n > max then set max to n
>> end repeat
>> return max
>>
>> end maximumFromList
>> </AppleScript>
>>
>>
>> On 2013/09/04, at 2:05, Alex Hall <email@hidden> wrote:
>>
>>> Thanks, everyone. The tip about sorting the cuts from largest to smallest, and thereby eliminating the offset moving problem, did it. I was hoping to avoid sorting at all, but insertion sort is fast and I'll only ever need to sort three to five items anyway. Here's what I came up with, and it works so far:
>>>
>>> on cutFromString(str, cutList)
>>> #str is the string to be cut up
>>> #cutList is a list of two-value lists, each the start and end index of a cut to make on str
>>> #cuts are inclusive, so {1,5} leaves characters 6 through end
>>> set cutList to insertionSortTupleList(cutList)
>>> set newStr to str
>>> repeat with cut in cutList
>>> set {startCut, endCut} to {item 1 of cut, item 2 of cut}
>>> if startCut > 1 then set newStr to (characters 1 through (startCut - 1) of newStr) as string
>>> if endCut < length of str then set newStr to (characters (endCut + 1) through -1 of newStr) as string
>>> end repeat
>>> return newStr as string
>>> end cutFromString
>>>
>>> on insertionSortTupleList(l)
>>> #this is based on the pseudocode found on
>>> #http://en.wikipedia.org/wiki/Insertion_sort
>>> repeat with i from 2 to (length of l)
>>> set itemToInsert to item i of l
>>> set insertionPoint to i
>>> repeat while insertionPoint > 1 and (item 1 of itemToInsert) > (item 1 of item (insertionPoint - 1) of l)
>>> set item insertionPoint of l to item (insertionPoint - 1) of l
>>> set insertionPoint to (insertionPoint - 1)
>>> end repeat
>>> set item insertionPoint of l to itemToInsert
>>> end repeat
>>> return l
>>> end insertionSort
>>>
>>> As to why I'm using strings for if statements, the series of scripts I have is for speaking information, such as time and date, wifi status, upcoming reminders, and so on. All things you can get in a glance at the status menus, but for people using Voiceover, accessing this information is a tedious process. Part of why I made these was to offer users the ability to quickly customize how things are read out, so the user can choose, for instance, how a date is spoken or whether he wants the date to be spoken before the time. Currently, scripts that have several possible paths have one template, then hard-coded strings for the less likely possibilities, or a bunch of similar-but-different keywords to try to keep track of. Now, I can let the user customize everything about a template, even using nested ifs, like this:
>>> ${$wifiOn$?${$wifiConnected$?You are connected to $ssid.$:You are not connected to any wifi networks.$}$:Wifi is currently disabled.$}
>>>
>>> The user can customize what happens if wifi is on or off, or connected or not, all in one template, and there are now less repetitive keywords to worry about mixing up.
>>>
>>> Thanks again for all the help and suggestions.
>>>
>>> On Sep 3, 2013, at 5:52 AM, Nigel Garvey <email@hidden> wrote:
>>>
>>>> Shane Stanley wrote on Tue, 03 Sep 2013 15:49:14 +1000:
>>>>
>>>>> On 03/09/2013, at 2:33 PM, Alex Hall <email@hidden> wrote:
>>>>>
>>>>>> I need a way to remove chunks from a string given the string and the
>>>> sets
>>>>>> of numbers, like {{1,3}, {8,16}}.
>>>>>
>>>>> on removeChunks_inString_(rangeList, theString)
>>>>> set rangeList to reverse of rangeList
>>>>> repeat with aRange in rangeList
>>>>> if item 1 of aRange = 1 then
>>>>> set theString to text ((item 2 of aRange) + 1) thru -1 of theString
>>>>> else if item 2 of aRange = (length of theString) then
>>>>> set theString to text 1 thru ((item 1 of aRange) - 1) of theString
>>>>> else
>>>>> set theString to text 1 thru ((item 1 of aRange) - 1) of theString &
>>>>> text ((item 2 of aRange) + 1) thru -1 of theString
>>>>> end if
>>>>> end repeat
>>>>> end removeChunks_inString_
>>>>>
>>>>> removeChunks_inString_({{1, 3}, {8, 16}}, "I need a way to remove chunks
>>>>> from a string given the string and the sets of numbers")
>>>>>
>>>>> It needs error checking, basically that item 2 of aRange is no greater
>>>> than
>>>>> the string's length.
>>>>
>>>> Also that the ranges are in order and don't overlap.
>>>>
>>>> Assuming that they are and don't, here's a less brain-taxing effort:
>>>>
>>>> on removeChunks_inString_(rangeList, theString)
>>>> set out to ""
>>>> set de to 1
>>>> repeat with aRange in rangeList
>>>> set ad to (beginning of aRange) - 1
>>>> if (ad > 0) then set out to out & text de thru ad of theString
>>>> set de to (end of aRange) + 1
>>>> end repeat
>>>> if (de ≤ (count theString)) then set out to out & text de thru -1 of theString
>>>> return out
>>>> end removeChunks_inString_
>>>>
>>>> removeChunks_inString_({{1, 3}, {8, 16}}, "I need a way to remove chunks from a string given the string and the sets of numbers")
>>>>
>>>>
>>>> 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
>>>
>>>
>>>
>>> Have a great day,
>>> Alex (msg sent from Mac Mini)
>>> email@hidden
>>>
>>>
>>>
>>> _______________________________________________
>>> 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
>>
>
>
>
> Have a great day,
> Alex (msg sent from Mac Mini)
> email@hidden
>
>
>
_______________________________________________
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