Next: , Previous: , Up: The Sample Extensions in the gawk Distribution   [Contents][Index]


17.7.9 Dumping and Restoring an Array

The rwarray extension adds four functions, named writea(), reada(), writeall() and readall(), as follows:

@load "rwarray"

This is how you load the extension.

ret = writea(file, array)

This function takes a string argument, which is the name of the file to which to dump the array, and the array itself as the second argument. writea() understands arrays of arrays. It returns one on success, or zero upon failure.

ret = reada(file, array)

reada() is the inverse of writea(); it reads the file named as its first argument, filling in the array named as the second argument. It clears the array first. Here too, the return value is one on success, or zero upon failure.

ret = writeall(file)

This function takes a string argument, which is the name of the file to which to dump the state of all variables. Calling this function is completely equivalent to calling writea(file, SYMTAB). It returns one on success, or zero upon failure

ret = readall(file)

This function takes a string argument, which is the name of the file from which to read the contents of various global variables. For each variable in the file, the data is loaded unless the variable already exists. If the variable already exists, the data for that variable in the file is ignored. It returns one on success, or zero upon failure.

The array created by reada() is identical to that written by writea() in the sense that the contents are the same. However, due to implementation issues, the array traversal order of the re-created array is likely to be different from that of the original array. As array traversal order in awk is by default undefined, this is (technically) not a problem. If you need to guarantee a particular traversal order, use the array sorting features in gawk to do so (see Controlling Array Traversal and Array Sorting).

The file contains binary data. All integral values are written in network byte order. However, double-precision floating-point values are written as native binary data. Thus, arrays containing only string data can theoretically be dumped on systems with one byte order and restored on systems with a different one, but this has not been tried.

Note that the writeall() and readall() functions provide a mechanism for maintaining persistent state across repeated invocations of a program. If, for example, a program calculates some statistics based on the data in a series of files, it could save state using writeall() after processing N files, and then reload the state using readall() when the N+1st file arrives to update the results.

Here is an example:

@load "rwarray"
…
ret = writea("arraydump.bin", array)
…
ret = reada("arraydump.bin", array)
…
ret = writeall("globalstate.bin")
…
ret = readall("globalstate.bin")

Next: Reading an Entire File, Previous: Two-Way I/O Example, Up: The Sample Extensions in the gawk Distribution   [Contents][Index]