![](/Images/NCL_NCAR_NSF_banner.png)
print_table
Prints formatted elements from a list to stdout.
Available in version 6.1.0 and later.
Prototype
procedure print_table ( alist [1] : list, format [1] : string )
Arguments
alistThe list of variables to print.
formatThe format string indicating how to print each item in the list.
Description
This procedure prints formatted elements from a list to stdout, using the given format string.
Use write_table if you want to send the formatted output to a file.
See Also
write_table, write_matrix, asciiwrite, sprintf, sprinti
Examples
Example 1
A simple example showing how to print an array of strings and integers:
strings = (/"string11","string12","string13"/) numbers = (/1,2,3/) alist = [/strings,numbers/] print_table(alist, "%s%d")The output will be:
string11 1 string12 2 string13 3
Note that write_table automatically inserted spaces between the strings and integers. If you don't want the spaces, then you will need to append the integers yourself:
strings = (/"string11","string12","string13"/) numbers = (/1,2,3/) alist = [/strings + numbers/] ; Use '+' to concatenate numbers to end of strings print_table(alist, "%s")
The output will now contain no spaces:
string111 string122 string133Example 2
This example shows how print several different types of variables (integer, float, string, shorts, longs) to the screen using formatted strings, and separated by commas:
a = (/111, 222, 333, 444/) b = (/1.1, 2.2, 3.3/) c = (/"a", "b", "c"/) d = (/11h, 22h/) f = (/11l, 22l, 33l, 44l, 55l, 66l/) alist = [/a, b, c, d, f/] print_table(alist,"%d,%16.2f,%s,%d,%ld")
Output will be:
111, 1.10,a,11,11 222, 2.20,b,22,22 333, 3.30,c, ,33 444, , , ,44 , , , ,55 , , , ,66
Example 3
This is similar to the previous example, except now an array of header strings are printed first, followed by the data, followed by an array of footer strings:
a = (/111, 222, 333, 444/) b = (/1.1, 2.2, 3.3/) c = (/"1", "22", "333", "aaaaaa", "bbbb", "cc"/) d = (/11h, 22h/) f = (/11l, 22l, 33l, 44l, 55l, 66l/) alist = [/a, b, c, d, f/] header = (/"--------------------------------", \ "This is a file header", \ "--------------------------------"/) footer = (/"--------------------------------", \ "This is a file footer", \ "--------------------------------"/) hlist = [/header/] flist = [/footer/] print_table(hlist, "%s") print_table(alist, "%d%16.2f%s%d%ld") print_table(flist, "%s")
Output will be:
-------------------------------- This is a file header -------------------------------- 111 1.10 1 11 11 222 2.20 22 22 22 333 3.30 333 33 444 aaaaaa 44 bbbb 55 cc 66 -------------------------------- This is a file footer --------------------------------
Example 4
This example shows how you can format integers of different numbers of digits (e.g. '95' versus '105' or '905' versus '1005') so they are pre-padded with zeros:
i = ispan( 8, 13, 1) j = ispan(80,130,10) print_table([/i,j/],"low_%04i high_%04i")
Output will be:
low_0008 high_0080 low_0009 high_0090 low_0010 high_0100 low_0011 high_0110 low_0012 high_0120 low_0013 high_0130
Example 5
The variable type must be appropriate for the format. If the user does not specify the width, then the function will use default settings.
sta = (/"Mum", "RNC", "CNB"/) ; type string Temp = (/30, 20, 25/) ; type integer RH = (/80, 900, 95/) WS = (/2, 3, 4/) WD = (/80, 150, 95/) Time = 201411050800l ; the appended 'l' makes this a 'long' int print_table([/Time/], "%li") print_table([/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ") print_table([/sta,Temp,RH,WS,WD/],"%s %i %i %i %i") ; change to float; use := syntax Temp := (/30, 20, 25/)*1.0 RH := (/80, 900, 95/)*1.0 WS := (/2, 3, 4/)*1.0 WD := (/80, 150, 95/)*1.0 print_table([/Time/], "%li") print_table([/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ") print_table([/sta,Temp,RH,WS,WD/],"%s %f %f %f %f")With default settings for %i, the output will look like:
201411050800 Station Temp RH WS WD Mum 30 80 2 80 RNC 20 900 3 150 CNB 25 95 4 95With default settings for %f, the output will look like:
201411050800 Station Temp RH WS WD Mum 30.000000 80.000000 2.000000 80.000000 RNC 20.000000 900.000000 3.000000 150.000000 CNB 25.000000 95.000000 4.000000 95.000000