11 November 2012

How to imitate two dimensional arrays in qlikview script

Below is a piece of qlikview script code which imitates two dimensional arrays:

2D array is supposed to be in the format like "MyArray" in the code below.
"GetItem" sub gets a item and assigns it to the given variable.
"FindItem" sub finds the item which corresponds to the given first dimension value and assigns it to the given variable.

Sub GetItem(fromArray,intoVar,X,Y)
Let firstDim=SubField(fromArray,',',X);
Let $(intoVar)=PurgeChar(SubField(firstDim,';',Y),Chr(39));
Set firstDim=NULL();
End Sub

Sub FindItem(fromArray,intoVar,SearchFor)
let k=fromArray;
For each t in $(fromArray)
Let s=t;
if SubField(t,';',1)=SearchFor then
Let $(intoVar)=SubField(t,';',2);
Exit for;
end if
End Sub

Set MyArray="'A;Turkey','C;Usa','D;Canada'";
Set vGetTest=Null();
Set vFindTest=Null();

Call GetItem(MyArray,'vGetTest',1,2); //This line sets 'vGetTest' to 'Turkey'
Call FindItem(MyArray,'vFindTest','C'); //This line sets 'vFindTest' to 'Usa'