gawk: Extension Sample Read write array
1
1 16.7.9 Dumping and Restoring an Array
1 -------------------------------------
1
1 The 'rwarray' extension adds two functions, named 'writea()' and
1 'reada()', as follows:
1
1 '@load "rwarray"'
1 This is how you load the extension.
1
1 'ret = writea(file, array)'
1 This function takes a string argument, which is the name of the
1 file to which to dump the array, and the array itself as the second
1 argument. 'writea()' understands arrays of arrays. It returns one
1 on success, or zero upon failure.
1
1 'ret = reada(file, array)'
1 'reada()' is the inverse of 'writea()'; it reads the file named as
1 its first argument, filling in the array named as the second
1 argument. It clears the array first. Here too, the return value
1 is one on success, or zero upon failure.
1
1 The array created by 'reada()' is identical to that written by
1 'writea()' in the sense that the contents are the same. However, due to
1 implementation issues, the array traversal order of the re-created array
1 is likely to be different from that of the original array. As array
1 traversal order in 'awk' is by default undefined, this is (technically)
1 not a problem. If you need to guarantee a particular traversal order,
11 use the array sorting features in 'gawk' to do so (⇒Array
Sorting).
1
1 The file contains binary data. All integral values are written in
1 network byte order. However, double-precision floating-point values are
1 written as native binary data. Thus, arrays containing only string data
1 can theoretically be dumped on systems with one byte order and restored
1 on systems with a different one, but this has not been tried.
1
1 Here is an example:
1
1 @load "rwarray"
1 ...
1 ret = writea("arraydump.bin", array)
1 ...
1 ret = reada("arraydump.bin", array)
1