5 Working with text

 

This chapter covers

  • Representing text with the String type
  • Formatting text with lpad and rpad
  • Reading text from a keyboard or file
  • Writing text to the screen or a file
  • Creating a simple interactive program

This chapter will focus on practical aspects of working with text in Julia, such as how to show text on the screen and read or write it to a file. You will also look at a simple interactive application where the user writes responses to questions.

However, first I will focus on different ways of displaying text on the screen, revisiting your pizza sales and sine table examples. The tables you created before were not very readable. How about creating a neat display like the one in figure 5.1?

Figure 5.1 Neatly formatted display of a cosine and sine table
05-01

Here sine and cosine values are neatly arranged in separate columns. Likewise, would it not be better to neatly organize information about pizza sales into clearly separated columns, as seen in figure 5.2?

Figure 5.2 Formatted display of pizza sales using alignment and colors in a Unix terminal window
05-02

You will use the printstyled function for coloring your text and the rpad and lpad functions for formatting the output. You will use the ^ operator to repeat characters. Next you will use the open function to allow you to use print and println for writing text output to files. To read and process input you will use the readline, split, and parse functions.

5.1 Making a pretty pizza sales table

5.1.1 Print, println, and printstyled

5.1.2 Printing multiple elements

5.1.3 Printing multiple pizzas

5.1.4 Align with lpad and rpad

5.1.5 Adding lines

5.2 Printing a trigonometric table

sitemap