• 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: How do I make words list from two lists?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I make words list from two lists?


  • Subject: Re: How do I make words list from two lists?
  • From: Nigel Garvey via AppleScript-Users <email@hidden>
  • Date: Mon, 23 Sep 2019 10:58:39 +0100

Shane Stanley wrote on Mon, 23 Sep 2019 09:51:37 +1000:

>On 23 Sep 2019, at 7:35 am, Nigel Garvey via AppleScript-Users
<email@hidden> wrote:

>> Alternatively, the letter list could be coerced to text and the
>> substrings extracted directly from it:
>
>Surprisingly -- to me at least -- that approach is actually a
*fraction* slower in a quick test here. I would have expected the
opposite.

So would I.  {8-/  But both took 0.000 seconds in my own quick tests so
I couldn't tell. The number and length of words extracted may play a
part.

>Here's a different approach that is quite a bit slower, but, well,
because:
>
>use AppleScript version "2.4"
>use scripting additions
>use framework "Foundation"
>
>set alist to {"T", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o",
"w", "n", "f", "o", "x"}
>set bList to {1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4}
>
>set string_list to {}
>set indexCounter to 1
>set startCounter to 1
>set theSet to current application's NSCountedSet's alloc()'s
initWithArray:bList
>repeat
>        set theCount to (theSet's countForObject:indexCounter)
>        if theCount = 0 then exit repeat
>        set end of string_list to (items startCounter thru
(startCounter + >theCount - 1) of alist) as text
>        set indexCounter to indexCounter + 1
>        set startCounter to startCounter + theCount
>end repeat

Nice!  :)  A minor refinement would be to lose three lines by making
indexCounter a repeat variable iterating from 1 to the last value in
bList and replace them with three lines for the TIDs stuff:

  use AppleScript version "2.4"
  use scripting additions
  use framework "Foundation"

  set alist to {"T", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w",
"n", "f", "o", "x"}
  set bList to {1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4}

  set string_list to {}
  set startCounter to 1
  set theSet to current application's NSCountedSet's alloc()'s
initWithArray:bList
  set astid to AppleScript's text item delimiters
  set AppleScript's text item delimiters to ""
  repeat with indexCounter from 1 to (end of bList)
    set theCount to (theSet's countForObject:indexCounter)
    set end of string_list to (items startCounter thru (startCounter + theCount
- 1) of alist) as text
    set startCounter to startCounter + theCount
  end repeat
  set AppleScript's text item delimiters to astid
  string_list

… or while we're having fun:  :)

  use AppleScript version "2.4"
  use scripting additions
  use framework "Foundation"

  set alist to {"T", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w",
"n", "f", "o", "x"}
  set bList to {1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4}

  set theArray to current application's NSMutableArray's arrayWithArray:alist
  set theSet to current application's NSCountedSet's alloc()'s
initWithArray:bList
  set c0 to current application's NSString's stringWithString:(character id 0)
  set breakPoint to (count alist)
  repeat with indexCounter from (end of bList) to (beginning of bList) + 1 by -1
    set breakPoint to breakPoint - (theSet's countForObject:indexCounter)
    (theArray's insertObject:c0 atIndex:breakPoint)
  end repeat
  set string_list to ((theArray's componentsJoinedByString:"")'s
componentsSeparatedByString:c0) as list


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

  • Follow-Ups:
    • Re: How do I make words list from two lists?
      • From: Shane Stanley via AppleScript-Users <email@hidden>
  • Prev by Date: Re: How do I make words list from two lists?
  • Next by Date: Re: How do I make words list from two lists?
  • Previous by thread: Re: How do I make words list from two lists?
  • Next by thread: Re: How do I make words list from two lists?
  • Index(es):
    • Date
    • Thread