Find: text

The find bar (Edit › Find…) searches this file's text as you type, with a live count of every match. total appears here as a class, a field, a method's tail (subtotal), a local, and inside a comment and a String. To play it in the IDE: Help > Lessons > Finding…, or ek9 -ide -lesson find-text.

The EK9 IDE as the lesson opens

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

shop.ek9

#!ek9
defines module lesson.finding.shop

  defines class

    Total
      total as Integer: 0

      addItem()
        -> price as Integer
        //total grows by one item's price
        total += price

      subtotal() as pure
        <- rtn as Integer: total

      default operator ?

  defines function

    priceLabel() as pure
      -> price as Integer
      <- rtn as String: `Total: ${price}`

    doubled() as pure
      -> price as Integer
      <- rtn as Integer: price * 2

  defines program

    ShowTotal()
      stdout <- Stdout()
      total <- Total()
      total.addItem(5)
      total.addItem(10)
      stdout.println(priceLabel(total.subtotal()))
      stdout.println("The running total is shown above")

//EOF

receipt.ek9

#!ek9
defines module lesson.finding.shop

  defines function

    receiptFooter() as pure
      -> grandTotal as Integer
      <- rtn as String: `Grand total: ${grandTotal}`

  defines program

    ShowReceipt()
      stdout <- Stdout()
      total <- Total()
      total.addItem(20)
      stdout.println(receiptFooter(total.subtotal()))

//EOF

till.ek9

#!ek9
defines module lesson.finding.till

  defines program

    ShowTill()
      stdout <- Stdout()
      total <- 0
      total += 15
      stdout.println(`Till total: ${total}`)

//EOF

Choose Edit > Find…

The EK9 IDE once this is done: Choose Edit > Find…

Type total into the field. The count updates as you type - 15 matches, comments and Strings included.

The EK9 IDE once this is done: Type total into the field

Step 2: Step through the matches

Choose the ↓ (or press Enter) to go to the next match.

The EK9 IDE once this is done: Choose the ↓ (or press Enter) to go to the next match

Choose the ↑ (or press Shift+Enter) to go back to the previous match.

The EK9 IDE once this is done: Choose the ↑ (or press Shift+Enter) to go back to the previous match

Step 3: Whole word

Turn on Whole word (W). total is one word, so it no longer matches inside subtotal - the count drops to 12.

The EK9 IDE once this is done: Turn on Whole word (W)

Step 4: Match case

Turn off Whole word, then turn on Match case (Aa). Capital-letter Total - the class itself, and the others spelled with a capital T - no longer match: 11 matches.

The EK9 IDE once this is done: Turn off Whole word, then turn on Match case (Aa)

Choose the ✕ (or press Escape) to close the find bar.

The EK9 IDE once this is done: Choose the ✕ (or press Escape) to close the find bar

The result

That is Text mode: it searches the file's text itself - comments and Strings included - never what the compiler knows. Match case and Whole word narrow it further; the next lesson uses a regular expression to search and replace in one pass.