Lesson 4: Printing a Table with Column Titles
In this lesson you will print a table with column titles. To achieve a presentable format with column titles requires a slightly more complicated approach.
To print a formatted version of the phone_data
table to the screen and place titles above each column, do the following:
1.Restore the phone data from a saved file.
RESTORE, !dir+'/data/phone_example.sav'
RESTORE, !dir+'\data\phone_example.sav'
2. Copy the following FOR loop to print the heading and phone data. The Format keyword in the PRINT statement uses FORTRAN-style format specifiers to format the rows.
FOR i=0, N_ELEMENTS(phone_data) - 1 DO BEGIN $
IF i EQ 0 THEN PRINT,'
DATE TIME DUR INIT'
+ $'
EXT COST AREA NUMBER'
&$
PRINT, Format='
(I6, 1X, I6, 3X, F5.2, 3X,'
+ $'
A3, 3X, I3, 2X, F5.2, 3X, I3, 3X, A10)'
, $
phone_data(i).DATE, phone_data(i).TIME, $
phone_data(i).DUR, phone_data(i).INIT, $
phone_data(i).EXT, phone_data(i).COST, $
phone_data(i).AREA, phone_data(i).NUMBER
This example prints the formatted phone_table
to the screen:
DATE
|
TIME
|
DUR
|
INIT
|
EXT
|
COST
|
AREA
|
NUMBER
|
---|---|---|---|---|---|---|---|
901002
|
93200
|
21.40
|
TAC
|
311
|
5.78
|
215
|
2154934242
|
901002
|
94700
|
1.05
|
BWD
|
358
|
0.0
|
303
|
2583869
|
901002
|
94700
|
17.44
|
EBH
|
320
|
4.71
|
214
|
2142319893
|
901002
|
94800
|
16.23
|
TDW
|
289
|
0.0
|
303
|
2955836
|
901002
|
94800
|
1.31
|
RLD
|
248
|
0.35
|
617
|
6174941999
|
901003
|
91500
|
2.53
|
DLH
|
332
|
0.68
|
614
|
6144695553
|
901003
|
91600
|
2.33
|
JAT
|
000
|
0.0
|
303
|
480344
|
901003
|
91600
|
.35
|
CCW
|
418
|
0.27
|
303
|
7725190
|
901003
|
91600
|
1.53
|
SRB
|
379
|
0.41
|
212
|
2123056618
|
901003
|
91600
|
.45
|
MLK
|
370
|
0.12
|
212
|
2124157956
|
901004
|
94700
|
.80
|
JAT
|
000
|
0.0
|
303
|
480320
|
901004
|
94900
|
1.93
|
SRB
|
379
|
0.52
|
818
|
8185012880
|
901004
|
95000
|
3.77
|
DJC
|
331
|
1.02
|
512
|
5125331228
|
901004
|
95100
|
.16
|
GWP
|
370
|
0.0
|
303
|
4441245
|
901004
|
95300
|
1.36
|
JAT
|
000
|
0.0
|
303
|
480320
|