Tables

Table is a core GreyCat type, that serves as a generic two-dimensional container. It is typically used to return a result set. For example, web components can handle Table objects returned by the GreyCat backend. Also, the Explorer can display Table objects.

Sampling results can also be expressed as Tables.

Data elements

Tables are populated one cell at a time. Not all cells need to contain values (null in this case).

fn main() {

  var t = Table::new(4);  // creates a table with 4 columns

  t.set(0, 0, 12335);
  t.set(0, 1, "onetwothree...");  // 1st row, 2nd column
  t.set(0, 2, Date::new(2022, 11, 22, 06, 35, 00, 0, TimeZone::Europe_Luxembourg));

  t.set(1, 1, "...threefive");
  t.set(1, 3, time::now());
  // other cells of 2nd row are null

  info(t.rows());  // 2

  t.remove(0, 1);  // removes row 0

  info(t.get(0, 1));  // "...threefive"
  info(t.get(0, 0));  // null
}

A Table can be sorted along one column.

Column information

The other essential aspect to Tables are their columns’ meta information. Each column is described by a TableColumnMeta object.

fn main() {
  var t = Table::new(2);
  t.setHeader(0, "N");

  t.set(0, 0, 10);
  t.set(1, 0, 20);

  var meta = t.getMeta(0);
  println(meta);
}

Displays: {"_type":"core.TableColumnMeta","type":"core.int","size":2,"index":false,"header":"N","min":10,"max":20,"avg":15,"std":7}.

use runtime;

fn main() {
  var t = Table::new(2);
  t.setHeader(0, "time");  // label a column

  t.set(0, 0, time::now());
  Runtime::sleep(1_s);  // sleep for 1 second
  t.set(1, 0, time::now());

  var meta = t.getMeta(0);
  println("column '${meta.header}' (${meta.type}): average of ${meta.size} values = ${meta.avg}");
}

Each column’s meta information also includes: min, max values, standard deviation. A column’s type is null whenever there are values of different types (or no values at all).