NCL Home >
Documentation >
Functions >
Unclassified routines
str_write
Writes all elements from a list (to a file).
Available in version 6.1.0 and later.
Prototype
procedure str_write ( filename [1] : string, option [1] : string, alist [1] : list, format [1] : string ) return_val [*] : string
Arguments
filenameThe file name to output the list.
optionThe option of writing the file. "w" for (over-)writing; "a" for appending.
alistThe input list to write.
formatThe format for of each (first) element of the list.
Description
Writes/Overwrites or appends elements to a file.
See Also
Examples
Example 1
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/]
str_write("list.txt", "w", hlist, "%s")
str_write("list.txt", "a", alist, "%d%16.2f%s%d%ld")
str_write("list.txt", "a", flist, "%s")
Where list.txt contains:
--------------------------------
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 2
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/]
str_write("list.txt", "w", hlist, "%s ")
str_write("list.txt", "a", alist, "%d,%16.2f,%s,%d,%ld")
str_write("list.txt", "a", flist, "%s ")
Output will be:
-------------------------------- 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 --------------------------------