Skip to content

save, rsave

Write global data to file or splayed to a directory

save

Write a global variable to file

save x     save[x]

Where x is a symbol atom or vector of the form [path/to/]v[.ext] in which

  • v is the name of a global variable
  • path/to/ is a file path (optional)
  • .ext is a file extension (optional)

writes global variable/s v etc. to file and returns the filename/s.

File path

If a file

  • exists, it is overwritten
  • does not exist, it is created, with any required parent directories

Format

The format used depends on the file extension:

(none)   binary
 csv     comma-separated values
 txt     plain text
 xls     Excel spreadsheet
 xml     Extensible Markup Language (XML)

There are no corresponding formats for load. Instead, use File Text.

Examples

q)t:([]x:2 3 5; y:`ibm`amd`intel; z:"npn")

q)save `t            / binary
`:t
q)read0 `:t
"\377\001b\000c\013\000\003\000\000\000x\000y\000z\000\000\..
"\000\003\000\000\000npn"

q)save `t.csv        / CSV
`:t.csv
q)read0 `:t.csv
"x,y,z"
"2,ibm,n"
"3,amd,p"
"5,intel,n"

q)save `t.txt        / text
`:t.txt
q)read0 `:t.txt      / columns are tab separated
"x\ty\tz"
"2\tibm\tn"
"3\tamd\tp"
"5\tintel\tn"

q)save `t.xls        / Excel
`:t.xls
q)read0 `:t.xls
"<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?>"
"<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" x...
q)save `t.xml        / XML
`:t.xml

q)read0 `:t.xml      / tab separated
"<R>"
"<r><x>2</x><y>ibm</y><z>n</z></r>"
"<r><x>3</x><y>amd</y><z>p</z></r>"
"<r><x>5</x><y>intel</y><z>n</z></r>"
"</R>"

q)save `$"/tmp/t"    / file path
`:/tmp/t

q)a:til 6
q)b:.Q.a
q)save `a`b          / multiple files
`:a`:b

Use set instead to save

  • a variable to a file of a different name
  • local data

rsave

Write a table splayed to a directory

rsave x     rsave[x]

Where x is a table name as a symbol atom, saves the table, in binary format, splayed to a directory of the same name. The table must be fully enumerated and not keyed.

File path

If the file

  • exists, it is overwritten
  • does not exist, it is created, with any required parent directories

Limits

The usual and more general way of doing this is to use set, which allows the target directory to be specified.

q)\l sp.q
q)rsave `sp           / save splayed table
`:sp/
q)\ls sp
,"p"
"qty"
,"s"

q)`:sp/ set sp        / equivalent to rsave `sp
`:sp/

set, .h.tx, .Q.dpft (save table), .Q.Xf (create file)
File system
Q for Mortals §11.2 Save and Load on Tables
Q for Mortals §11.3 Splayed Tables