Extension Sample Read write array (The GNU Awk User’s Guide)

From Get docs
Gawk/docs/latest/Extension-Sample-Read-write-array


17.7.9 Dumping and Restoring an Array

The rwarray extension adds two functions, named writea() and reada(), 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.

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 section 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.

Here is an example:

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