• 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: list manipulation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: list manipulation


  • Subject: Re: list manipulation
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 14 Apr 2017 12:26:32 -0500

On 04/14/2017, at 10:30, Jean-Christophe Helary <email@hidden> wrote:
I was just going to ask "Also, is AS able to call a handler on itself ?" when I checked the web, and...


Let's actually provide the code for people to chew on and so it can be found when searching the archives.

From Matt Neuburg's book AppleScript: The Definitive Guide, 2nd Edition

** NOTE the built-in AppleScript filter-form I've added.

------------------------------------------------------------------------------
# Auth: Christopher Stone (refactored from a handler by Matt Neuburg)
# dCre: 2017/04/14 11:49
# dMod: 2017/04/14 11:58
# Appl: Miscellaneous
# Task: Recursive Handler for filtering Numbers.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @List, @Manipulate, @Recursive, @Recursion, @Handler, @Filter, @Numbers
------------------------------------------------------------------------------

set theList to {"hey", 1, "ho", 2, 3}

set filteredList1 to filterNumbers(theList) -- handler

set filteredList2 to numbers of theList -- built-in AppleScript filter form

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on filterNumbers(theList)
    if theList = {} then return theList
    if {class of item 1 of theList} is in {real, integer, number} then
        return {item 1 of theList} & (filterNumbers(rest of theList))
    else
        return filterNumbers(rest of theList)
    end if
end filterNumbers
------------------------------------------------------------------------------





And I also found CAR: item 1 of list and  CDR: rest of list

For people not familiar with LISP functions:

CAR — takes a list as argument, and returns its first element.
CDR – takes a list as argument, and returns a list without the first element.





Don't forget positive and negative indexing.

set theList to {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}

set newList1 to items 2 thru 4 of theList
set newList2 to items -3 thru -1 of theList
set newList3 to items 2 thru -1 of theList -- equivalent to “rest of”


--
Best Regards,
Chris

 _______________________________________________
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

References: 
 >list manipulation (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: list manipulation
  • Next by Date: Re: list manipulation
  • Previous by thread: Re: list manipulation
  • Next by thread: Re: list manipulation
  • Index(es):
    • Date
    • Thread