• 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: Best Method to Filter Lists?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best Method to Filter Lists?


  • Subject: Re: Best Method to Filter Lists?
  • From: Jim Underwood <email@hidden>
  • Date: Fri, 10 Jun 2016 22:57:26 +0000
  • Thread-topic: Best Method to Filter Lists?

My thanks to Chris and Yvan for their suggestions.

I've updated and expanded the filterList()   script.
All comments and suggestions welcomed.

Script for on filterList()



(*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  filterList Handler Demo

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

VER:   1.1    LAST UPDATE:   2016-06-10


PURPOSE:

  • Demo how my filterList() handler works


AUTHOR:    JMichaelTX


REQUIRED:

  1.  Mac OS X Yosemite 10.10.5+



REF:  The following were used in some way in the writing of this script.

  1.  List Manipulation Routines

    http://www.macosxautomation.com/applescript/sbrt/sbrt-07.html

  2. AppleScript Users List (ASUL)

    • Feedback and suggestions

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   

*)


set myList to {"@Test", "other", "@end", "else", "other", "@Test"}


set startsList to my filterList(myList, "@", "starts with")

set endsList to my filterList(myList, "end", "ends with")

set containsList to my filterList(myList, "the", "contains")

set replaceList to my filterList(myList, {"@Test", "@Done"}, "replace")

set removeList to my filterList(myList, "other", "remove")

set removeDupsList to my filterList(myList, "other", "remove dups")

set removeAllDupsList to my filterList(myList, "", "remove all dups")


set containsList to my filterList(myList, "the", "bad action") ## will cause error


--~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on filterList(pList, pMatch, pMatchAction)

  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  (*

  VER:   1.1    LAST UPDATE:   2016-06-10


  RETURNS:  List filtered by the requested pMatchAction

  

  PARAMETERS:

    • pList      : Source List of text items

    • pMatch      : Can be String, or List of two text items

                • Use String to search List for all pMatchActions, except:

                • Use List of {"MatchStr", "ReplaceStr"} for "replace" action

                

    • pMatchAction   : Action to perform on found items

      Must be one of these strings:

      • starts with      -- returns only items that start with pMatch

      • ends with        -- returns only items that end with pMatch

      • contains         -- returns only items that contain pMatch

      • replace          -- returns all items, but replace items that 

                            exactly match item 1 of pMatch with item 2 of pMatch

      • remove           -- returns only items that do not match pMatch

      • remove dups      -- returns all items, except dups of item 

                            that matches pMatch

      • remove all dups  -- returns all items, except removes dups of all items 

                            regardless of match.



  AUTHOR:    JMichaelTX (with help from friends at ASUL)


  Since lists don't support "whose" or "where" clauses,

  have to roll my own.

  –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

  *)

  

  

  local foundItems, oItem, pMatchStr, pReplaceStr

  

  if (class of pMatch) is list then

    set pMatchStr to text of (item 1 of pMatch)

    set pReplaceStr to text of (item 2 of pMatch)

  else

    set pMatchStr to pMatch

  end if

  

  set foundItems to {}

  

  repeat with oItem in pList

    

    if (pMatchAction = "starts with") then

      if (oItem starts with pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "ends with") then

      if (oItem ends with pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "contains") then

      if (oItem contains pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "replace") then

      if (text of oItem = pMatchStr) then

        set end of foundItems to pReplaceStr

      else

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "remove") then

      if (text of oItem ≠ pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "remove dups") then

      if (text of oItem = pMatchStr) then

        if (oItem is not in foundItems) then

          set end of foundItems to text of oItem

        end if

      else

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchAction = "remove all dups") then

      if (oItem is not in foundItems) then

        set end of foundItems to text of oItem

      end if

      

    else ### ERROR ###

      error "Invalid pMatchAction in filterList handler:" & return & "»»»" & pMatchAction & ¬

        "«««" & return & return & ¬

        "Must be one of the following:" & return & "starts with, ends with, contains, replace, remove, remove dups, remove all dups"

    end if

  end repeat

  

  return foundItems

  

end filterList

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





Best Regards,

Jim Underwood
aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of Chris Stone <email@hidden>
Date: Fri, Jun 10, 2016 at 4:43 AM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Re: Best Method to Filter Lists?

Hey Jim,

Many have been written over the years.

Your handler looks pretty good.

Yvan's method of transforming a list in place works well.

Another handy tool is to use filter-forms that list-objects do understand.
 _______________________________________________
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: 
 >Best Method to Filter Lists? (From: Jim Underwood <email@hidden>)
 >Re: Best Method to Filter Lists? (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Best Method to Filter Lists?
  • Next by Date: Re: Getting Individual Metadata Items with ASObjC
  • Previous by thread: Re: Best Method to Filter Lists?
  • Next by thread: Re: Best Method to Filter Lists?
  • Index(es):
    • Date
    • Thread