Filtering one list out of another

Geoff Canyon gcanyon at gmail.com
Wed Dec 25 19:13:53 EST 2013


On Tue, Dec 24, 2013 at 2:29 AM, Devin Asay <devin_asay at byu.edu> wrote:
>
>
> This is what I ended up with:
>
> function filterList1WithoutList2 pMainList, pExcludeList
>     put pMainList into tMain
>     repeat for each line tLine in pExcludeList
>         set the wholeMatches to true
>         put lineOffset(tLine, tMain) into tMatchedLine
>         if tMatchedLine > 0 then
>             delete line tMatchedLine of tMain
>         end if
>     end repeat
>     return tMain
> end filterList1WithoutList2


If your main list has any duplicate lines that need to be removed, the code
above will remove only one of them. This removes duplicates, but de-dupes
everything; if 3 is to be removed, it removes all 3s but if 3 isn't to be
removed and the original list has four 3s, it will return just one of them:

*function* filterWithout pMain, pExclude

   *split* pMain using cr as *set*

   *repeat* for each line tLine in pExclude

      *delete* variable pMain[tLine]

   *end* *repeat*

   *return* the keys of pMain

*end* filterWithout


This removes any duplicate lines that need to be removed, but leaves other
duplicates in place. It also retains the order of the original list, which
the one above doesn't:


*function* filterWithout pMain, pExclude

   *split* pExclude using cr as *set*

   *repeat* for each line tLine in pMain

      *if* pExclude[tLine] *then* *next* *repeat*

      *put* tLine & cr after R

   *end* *repeat*

   *return* char 1 to -2 of R

*end* filterWithout

For your original routine above, you can speed it up a bit by putting the
wholematches before the repeat. Each of these three routines can be faster
or slower depending on the contents of the two lists:

*function* filterList1WithoutList2 pMainList, pExcludeList

   *set* the wholeMatches to true

   *repeat* for each line tLine in pExcludeList

      *get* lineOffset(tLine, pMainList)

      *if* it > 0 *then* *delete* line it of pMainList

   *end* *repeat*

   *return* pMainList

*end* filterList1WithoutList2



More information about the Use-livecode mailing list