This tutorial explains Nim Tables and different examples.

Nim Tables

Nim tables are data structures used to store keys and values. These are also called HashTable and dictionaries in Java and Go language.

Points:

  • Contains key and value pairs
  • Elements order is not guaranteed
  • use orderedTable to preserve the order of key and value pairs
  • Empty table is declared with {:}

How to declare and initialize Table in Nim

Two ways we can declare a table in nim.

  • using initTable
  • using toTable directly

using initTable:

Syntax:

var variable = initTable[keyType,valueType]()

initTable is a keyword to create a table

key and value types are datatypes in NIM.

Here is an example to declare a table of key type int and value type string.

import tables
var employees = initTable[int,string]()
echo employees

Output:

{:}

using toTable with values assigned: In this syntax, key and value pairs are initialized directly using the toTable syntax.

Types are inferred using the values on the right-hand side.

var numbers = {1: "employee1", 2: "employee2", 3: "employee3"}.toTable
echo numbers

How to add data to a table in Nim

key and value pairs added to using key and value syntax. syntax:

variable[key]=value

Here is an example

import tables
var employees = initTable[int,string]()

employees[1] = "employee1"
employees[2] = "employee2"
employees[3] = "employee3"
echo employees
{3: "employee3", 2: "employee2", 1: "employee1"}

How to delete key and value from a table in Nim

del proc delete key and value with given key value

Here is an example

import tables
var employees = initTable[int,string]()

employees[1] = "employee1"
employees[2] = "employee2"
employees[3] = "employee3"

echo employees
employees.del(1)
echo employees

Output:

{3: "employee3", 2: "employee2", 1: "employee1"}
{3: "employee3", 2: "employee2"}

Nim Table length

use len proc to know about the number of pairs of elements in a table.

import tables
var employees = initTable[int,string]()

employees[1] = "employee1"
employees[2] = "employee2"
employees[3] = "employee3"

echo employees
echo employees.len

Output:

{3: "employee3", 2: "employee2", 1: "employee1"}
3

How to check if Key exists in Nim Tables

One of the requirements is to check whether a key exists or not.

hasKey proc checks whether a key exists in a table or not. Returns true if the key exists, else returns false.

Here is an example

import tables
var employees = initTable[int,string]()

employees[1] = "employee1"
employees[2] = "employee2"
employees[3] = "employee3"

echo  employees.hasKey(1)
echo  employees.hasKey(11)

Output:

true
false

NIm Tables iterate key and values

Sometimes, Want to iterate the key and values of a table.

for loop used to iterate pairs. each iteration, a pair of keys and values are stored and printed to the console.

Here is an example

import tables
var employees = initTable[int,string]()

employees[1] = "employee1"
employees[2] = "employee2"
employees[3] = "employee3"

for id, name in employees:
    echo id, " -  ", name

Output:

3 -  employee3
2 -  employee2
1 -  employee1