Qlik Community

QlikView Documents

Documents for QlikView related information.


Breathe easy -- you now have more time to plan your next steps with Qlik!
QlikView 11.2 Extended Support is now valid through December 31, 2020. Click here for more information.

Using QlikView Expressor Datascript Tables

Not applicable

Using QlikView Expressor Datascript Tables

Originally published on 07-21-2011 09:44 AM

Expressor Datascript tables are extremely powerful and useful and you must understand how to use these tables in your code.  Fortunately, basic uses of the table are easy to master and the more complex usages are not that much more difficult.

Let’s consider the basic numeric indexed table.  In this usage, index values start, by default, at one, although you can specify zero or negative numbers as the index value.  If you want to accept the default indexing scheme, you can declare and initialize the table in a single statement.

myTable = {val1, val2, val3, ...}

If val is a numeric, you simply enter it.  If val is a string, it must be enclosed in quotation marks ("").  To retrieve a value, you specify the index of the value you want.

myTable[1] returns val1, while myTable[2] and myTable[3] return val2 and val3

If you want to set the index value, you must explicitly provide it.

myTable = {[-1]=val1, [-2]=val3, val3, ...}

In this case, val1 is associated with the index -1, val2 with the index -2, and val3 with the index 1.  You are free to intermix specified and default indices.

myTable = {[-1]=1, [-2]=2, 3, [-3]=4, 5}

Although you can use zero or negative numbers as indices, this is not a common practice as it makes retrieving values from the table more difficult.

Table indices are not limited to numbers, strings are acceptable too.  But there are a few rules you need to observe.

  • If the index starts with an alphabetic character and includes only alphanumeric characters, it may be entered without surrounding quotation marks.
    myTable = {first=val1, s2=val2}
    • To retrieve the value you can use one of two notations.

      myTable["first"] or myTable.s2

  • If the index starts with a numeric or non-alphabetic character, or includes a non-alphanumeric character, it must be entered with surrounding quotation marks within a set of square braces.
    myTable = {["1"]=val1, ["2!"]=val2}
    • To retrieve the value you must quote the index value:
      myTable["1"] or myTable["2!"]

Of course, you can always declare the table first and then add the elements.

myTable = { }
myTable[1] = val1
myTable["2!"] = val2

Note that with this approach, you must always provide the index.  Note also, that you are free to mix numeric and string indices in the same table.

A table may hold complex types, for example, other tables.

Table1 = {1,2}
Table2 = {first=Table1, second = {3,4}}

In Table2, the index first references Table1 while the index second references an unnamed table.  To retrieve one of the numeric values, you must specify two indices.

Table2.first[1], which returns the value 1
Table2.second[2], which returns the value 4

Or consider

Table2 = {Table1}

where 1 is the index in Table2 that corresponds to Table1.  To retrieve values, use a two-dimensional index:

Table2[1][2], which returns the value 2

And perhaps one of the most interesting uses of a table is to hold functions.

myTable = { }
function myTable.larger(a,b) return (a>b) and a or b end


myTable.larger(4,5) returns 5
myTable.larger(4,3) returns 4

Here, the table myTable uses the index larger  to locate the code that returns the greater of two numbers.  This is a  nice technique to use to build up your own collection of functions.  In  fact, this is exactly how the functions in the string, datetime, or  other groupings have been defined.

So, how do you retrieve values from a table?  As you saw above, you can always provide the index of the element you want to retrieve.  But what if you don’t know the index, is there a way to work through a table, retrieving values?  Yes.

Datascript includes two iterator functions that are designed to walk through a table returning each index and its associated value.

With a numeric indexed table that uses the default indexing scheme, this iterator function is named ipairs.

for index, value in ipairs(myTable) do
   -- manipulate index and value in some way

This code block will retrieve each value from the table starting with the  value specified through the index 1.  Values associated with zero or a  negative numeric index or with a string index will not be retrieved.

With a table that uses string and/or numeric indices, even negative numeric indices, the iterator function pairs will walk through the table.

for index, value in pairs(myTable) do
   -- manipulate index and value in some way

You will find many uses for tables when working with Datascript.  In fact, any time you need to create a collection or want to implement an efficient way to carry out a series of comparisons, such as nested if..then..else statements, or perform a lookup, think table.

Version history
Revision #:
1 of 1
Last update:
‎07-23-2012 07:25 PM
Updated by: