9.38 KeyedAccessFileΒΆ

The domain KeyedAccessFile(S) provides files which can be used as associative tables. Data values are stored in these files and can be retrieved according to their keys. The keys must be strings so this type behaves very much like the StringTable(S) domain. The difference is that keyed access files reside in secondary storage while string tables are kept in memory. For more information on table-oriented operations, see the description of Table.

Before a keyed access file can be used, it must first be opened. A new file can be created by opening it for output.

ey: KeyedAccessFile(Integer) := open("/tmp/editor.year", "output")
\[\]
“/tmp/editor.year”

Type: KeyedAccessFile Integer

Just as for vectors, tables or lists, values are saved in a keyed access file by setting elements.

ey."Char" := 1986
\[\]
1986

Type: PositiveInteger

ey."Caviness" := 1985
\[\]
1985

Type: PositiveInteger

ey."Fitch" := 1984
\[\]
1984

Type: PositiveInteger

Values are retrieved using application, in any of its syntactic forms.

ey."Char"
\[\]
1986

Type: PositiveInteger

ey("Char")
\[\]
1986

Type: PositiveInteger

ey "Char"
\[\]
1986

Type: PositiveInteger

Attempting to retrieve a non-existent element in this way causes an error. If it is not known whether a key exists, you should use the searchsearchKeyedAccessFile operation.

search("Char", ey)
\[\]
1986

Type: Union(Integer,...)

search("Smith", ey)
\[\]
“failed”

Type: Union(“failed”,...)

When an entry is no longer needed, it can be removed from the file.

remove!("Char", ey)
\[\]
1986

Type: Union(Integer,...)

The keyskeysKeyedAccessFile operation returns a list of all the keys for a given file.

keys ey
\[\]
[“Fitch”,”Caviness”]

Type: List String

The # #KeyedAccessFile operation gives the number of entries.

#ey
\[\]
2

Type: PositiveInteger

The table view of keyed access files provides safe operations. That is, if the FriCAS program is terminated between file operations, the file is left in a consistent, current state. This means, however, that the operations are somewhat costly. For example, after each update the file is closed.

Here we add several more items to the file, then check its contents.

KE := Record(key: String, entry: Integer)
\[\]
Record(key:String,entry:Integer)

Type: Domain

reopen!(ey, "output")
\[\]
“/tmp/editor.year”

Type: KeyedAccessFile Integer

If many items are to be added to a file at the same time, then it is more efficient to use the writewriteKeyedAccessFile operation.

write!(ey, ["van Hulzen", 1983]$KE)
\[\]
[key=”vanHulzen”,entry=1983]

Type: Record(key: String,entry: Integer)

write!(ey, ["Calmet", 1982]$KE)
\[\]
[key=”Calmet”,entry=1982]

Type: Record(key: String,entry: Integer)

write!(ey, ["Wang", 1981]$KE)
\[\]
[key=”Wang”,entry=1981]

Type: Record(key: String,entry: Integer)

close! ey
\[\]
“/tmp/editor.year”

Type: KeyedAccessFile Integer

The readreadKeyedAccessFile operation is also available from the file view, but it returns elements in a random order. It is generally clearer and more efficient to use the keyskeysKeyedAccessFile operation and to extract elements by key.

keys ey
\[\]
[“Wang”,”Calmet”,”vanHulzen”,”Fitch”,”Caviness”]

Type: List String

members ey
\[\]
[1981,1982,1983,1984,1985]

Type: List Integer

)system rm -r /tmp/editor.year

For more information on related topics, see FileXmpPage , TextFileXmpPage , and LibraryXmpPage .