write_table
Writes formatted elements from a list to an ASCII file.
 Available in version 6.1.0 and later.
Prototype
procedure write_table ( filename [1] : string, option [1] : string, alist [1] : list, format [1] : string )
Arguments
filenameThe ASCII file name for sending the formatted output.
optionOption for how to write to the file: "w" to overwrite, "a" to append.
alistThe list of variables to write to the file.
formatThe format string indicating how to write each item in the list.
Description
This procedure writes or appends formatted elements from a list to the given ASCII file, using the given format string.
Use print_table if you simply want to print the formatted values to the screen (stdout).
See Also
print_table, write_matrix, asciiwrite, sprintf, sprinti
Examples
Example 1
A simple example showing how to write an array of strings and integers to a file:
  strings = (/"string11","string12","string13"/)
  numbers = (/1,2,3/)
  alist   = [/strings,numbers/]
  write_table("example1.txt", "w", alist, "%s")
The example1.txt file will contain:
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
  write_table("example1.txt", "w", alist, "%s")
The example1.txt file will now contain no spaces:
string111 string122 string133
Example 2
This example shows how to initially create a file with the "w" option, and then append data to it using the "a" option. This is useful for creating a file with header text, data, and footer text.
 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/]
 fname = "example2.txt"
 write_table(fname, "w", hlist, "%s")
 write_table(fname, "a", alist, "%d%16.2f%s%d%ld")
 write_table(fname, "a", flist, "%s")
The example2.txt file will contain:
--------------------------------
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 3
This is similar to the previous example, except now commas are inserted in the data fields.
 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/]
 header = (/"--------------------------------", \
            "This is a file header", \
            "--------------------------------"/)
 footer = (/"--------------------------------", \
            "This is a file footer", \
            "--------------------------------"/)
 hlist = [/header/]
 flist = [/footer/]
 fname = "example3.txt"
 write_table(fname, "w", hlist, "%s ")
 write_table(fname, "a", alist, "%d,%16.2f,%s,%d,%ld")
 write_table(fname, "a", flist, "%s ")
The example3.txt file will contain:
-------------------------------- This is a file header -------------------------------- 111, 1.10,a,11,11 222, 2.20,b,22,22 333, 3.30,c, ,33 444, , , ,44 , , , ,55 , , , ,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( 95,105, 1) j = ispan(905,1005,10) write_table("example4.txt","w",[/i,j/],"first_%05i second_%05i")
The example4.txt file will contain:
first_00095 second_00905 first_00096 second_00915 . . . first_00099 second_00945 first_00100 second_00955 first_00101 second_00965 first_00102 second_00975 . . . first_00105 second_01005
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 = 201411050800>b>l             ; the appended 'l' makes this a 'long' int
; Temp, RH, WS, WD are all integers
       file_I = "example5_I.txt"
       system("/bin/rm -f "+file_I)   ; remove any pre-existing file
       write_table(file_I, "w", [/Time/], "%li")
       write_table(file_I, "a", [/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ")
       write_table(file_I, "a", [/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
        file_F = "example5_F.txt"
        system("/bin/rm -f "+file_F)   ; remove any pre-existing file
        write_table(file_F, "w", [/Time/], "%li")
        write_table(file_F, "a", [/"Station","Temp","RH","WS","WD"/], "%s %s %s %s %s ")
        write_table(file_F, "a", [/sta,Temp,RH,WS,WD/],"%s %f %f %f %f")
With default settings for %i, the example5_I.txt file will contain:
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 example5_F.txt file will contain:
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