LessonsIntermediate: Stacks, objects and dataLesson 31

Filtering lists

Keep only the lines or items that match a pattern, or throw them away, with wildcards or regular expressions, and remove duplicates.

  • Intermediate
  • Lesson 31 of 83
  • About 8 minutes
  • 7 examples

Keep what matches

filter goes through a list and keeps only the lines that match a pattern. In a pattern, * stands for any run of characters (or none) and ? for exactly one:

ExampleRun it in the playground
put "apple" & return & "banana" & return & "cherry" & return & "avocado" into tFruit
filter tFruit with "a*"
put tFruit
Output
apple
avocado

without does the opposite, and throws the matches away:

ExampleRun it in the playground
put "apple" & return & "banana" & return & "cherry" & return & "avocado" into tFruit
filter tFruit without "a*"
put tFruit
Output
banana
cherry

Wildcard patterns

The pattern must match the whole line. Put * on both sides to find lines that contain something anywhere:

ExampleRun it in the playground
put "x1" & return & "y22" & return & "x333" & return & "box" into tCodes
filter tCodes with "x?" into tShort
put tShort
filter tCodes with "*x*" into tAnyX
put tAnyX
Output
x1
x1
x333
box

into puts the result in another variable and leaves the list as it was, so you can filter the same list several ways. Square brackets match one of a set of characters: [aeiou]* keeps lines that start with a vowel.

Items, and empty lines

Say items of to filter a comma-separated list. A very common job is dropping the blank lines from text: filter it without empty.

ExampleRun it in the playground
put "cat,dog,cow,horse" into tAnimals
filter items of tAnimals with "c*"
put tAnimals
put "one" & return & return & "two" & return & return & "three" into tText
filter tText without empty
put tText
Output
cat,cow
one
two
three

Filtering with regular expressions

For patterns a wildcard cannot say, filter with a regular expression. Unlike a wildcard, a regular expression matches anywhere in the line unless you anchor it with ^ (the start) or $ (the end). The lesson Regular expressions teaches the language of these patterns.

ExampleRun it in the playground
put "alpha" & return & "beta" & return & "gamma" & return & "delta" into tWords
filter tWords with regex pattern "^[ab]"
put tWords
put "order 66" & return & "no number" & return & "room 101" into tLines
filter tLines with regex pattern "[0-9]+$"
put tLines
Output
alpha
beta
order 66
room 101

Removing duplicates

Filtering cannot see whether a line has appeared before, but an array can: each value becomes a key, and a key can only be there once. split with as set does exactly that, and the keys gives the list back. The keys of an array come in no particular order, so sort them:

ExampleRun it in the playground
put "pear" & return & "fig" & return & "pear" & return & "apple" & return & "fig" into tList
split tList by return as set
put the keys of tList into tUnique
sort lines of tUnique
put tUnique
Output
apple
fig
pear

Arrays are the subject of the next two lessons.

Try it yourself

From this list of files, keep only the pictures, those ending in .png or .jpg, sorted by name: notes.txt, cat.png, song.mp3, beach.jpg, apple.png.

Show a solution
ExampleRun it in the playground
put "notes.txt,cat.png,song.mp3,beach.jpg,apple.png" into tFiles
replace comma with return in tFiles
filter tFiles with regex pattern "\.(png|jpg)$"
sort tFiles
put tFiles
Output
apple.png
beach.jpg
cat.png

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.