Justifying Output

The format command also gives us a lot of control over text justification. Control sequences allow us to format tables, center text, and perform other useful justification feats.

To help you understand the various justification rules, we’ll create a simple function that returns different animal names with varying character lengths:

> (defun random-animal ()
      (nth (random 5) '("dog" "tick" "tiger" "walrus" "kangaroo")))
RANDOM-ANIMAL
> (random-animal)
"walrus"

Now suppose we want to display a bunch of random animals in a table. We can do this by using the ˜t control sequence. ˜t can take a parameter that specifies the column position at which the formatted value should appear. For example, to have our table of animals appear in three columns at the fifth, fifteenth, and twenty-fifth character positions, we could create this table:

> (loop repeat 10
        do (format t "˜5t˜a ˜15t˜a ˜25t˜a˜%"
                   (random-animal)
                   (random-animal)
                   (random-animal)))
     kangaroo  tick      dog
     dog       walrus    walrus
     walrus    tiger     tiger
     walrus    kangaroo  dog
     kangaroo  tiger     dog
     tiger     walrus    kangaroo
     tick      dog       tiger
     kangaroo  tick      kangaroo
     tiger     dog       walrus
     kangaroo  kangaroo  tick

Remember that a loop command with a repeat 10 clause executes the body of the loop 10 times. As you can see, use of the ˜t control sequence caused the animals to be laid out in a neatly formatted table.

image with no caption

Now suppose we want all the animals be spaced equally apart on a single line. To do so, we can use the ˜< and ˜> control sequences, as follows:

> (loop repeat 10
        do (format t "˜30<˜a˜;˜a˜;˜a˜>˜%"
                   (random-animal)
                   (random-animal)
                   (random-animal)))
tick         tiger        tick
tick         tiger         dog
tick          dog          dog
kangaroo     kangaroo    tiger
tiger      tiger      kangaroo
walrus       kangaroo      dog
dog         dog         walrus
kangaroo       dog      walrus
walrus        dog       walrus
kangaroo       tiger      tick

Let’s deconstruct this control string to understand how it works:

image with no caption

First, the ˜30< tells the function that we’re initiating a block of justified text. The parameter 30 indicates that the block should be 30 characters wide. Next, we have three ˜a control sequences in a row, one for each animal. Each ˜a is separated by ;, which tells format that we’re starting a new value to be justified by ˜<. (The ˜; sequences indicate where extra spaces should be inserted to justify the values.) We then end the justified section with the ˜> command sequence.

Because the equal spacing of the animals in each line doesn’t guarantee that the columns created by printing multiple lines will be properly aligned, we add the :@ flag to our justification ˜< command sequence. For example, we can create a single, neatly centered column as follows:

> (loop repeat 10 do (format t "˜30:@<˜a˜>˜%" (random-animal)))
              dog
            walrus
           kangaroo
             tick
             tick
             tiger
              dog
           kangaroo
           kangaroo
              dog

In the same way, we can use :@ with multiple justified values, centering them on the line with additional space at their left and right ends:

> (loop repeat 10
        do (format t "˜30:@<˜a˜;˜a˜;˜a˜>˜%"
                     (random-animal)
                     (random-animal)
                     (random-animal)))
    walrus    tick    tick
    walrus    tiger    tick
     tick     dog     tick
    walrus    tiger   tiger
   kangaroo   dog   kangaroo
   tiger   kangaroo   walrus
   tiger  kangaroo  kangaroo
    kangaroo   tiger   tick
    tick    tiger    walrus
    walrus    tiger    tick

This step brings us closer to having three neatly centered columns, but our columns are still a bit wavy because we’re aligning the values within a single line, without telling format to arrange the values using three centered columns.

To produce neat columns, we’ll still use the :@ flag, but we’ll describe our rows using three separate 10-character justification sections:

> (loop repeat 10
        do (format t "˜10:@<˜a˜>˜10:@<˜a˜>˜10:@<˜a˜>˜%"
                     (random-animal)
                     (random-animal)
                     (random-animal)))
   tiger   kangaroo  kangaroo
 kangaroo  kangaroo   walrus
   tick      tick      tick
    dog       dog       dog
   tiger      dog     walrus
    dog      tiger   kangaroo
  walrus      dog      tick
   tick     walrus   kangaroo
    dog      tick     walrus
   tiger     tiger     tiger

At last, we have the nicely centered random animal columns of our dreams!

As you can see, the layout options for format are quite flexible. Since we often need to create complex lists and tables of data when debugging applications, these tricks are very helpful when you need to get a handle on your data, even with more complex programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228