2.5 Working with Associative Arrays
It is often necessary to process asssociative arrays entirely or to delete individual associations which are no longer needed. With help of the attribute .itemcount it is possible to query the number of available items.
window WMain
{
integer channel [ string ];
.channel [ "MTV" ] := 65;
.channel [ "CNN" ] := 14;
.channel [ "NBC" ] := 11;
}
rule integer StationNumber()
{
return ( WMain.itemcount[ .channel ]); !! in example, 3
}
2.5.1 Method index
It is also possible to query the internal order of associative arrays. Dialog Manager uses the internal structure for administration purposes only, in order to run through its associative array.
Attention
The internal structure of associate arrays is used for the internal administration only and may change. If you need structured data, which means that the associative arrays have been misused, you must calculate it yourself. In this case, however, we recommend using normal arrays.
Dialog Manager offers the possibility to run through an associative array entirely. Please do note that you may calculate the index from the internal structure by using the method :index().
<Object>:index ( attribute <Name>, integer <InnerIndex> );
<Object> is the object for which the array <Name> is defined. <InnerIndex> is the index specifying the internal structure which is defined with integer numbers from 1 up to the number of items.
Example
window WMain
{
integer channel [ string ];
.channel [ "MTV" ] := 65;
.channel [ "CNN" ] := 14;
.channel [ "NBC" ] := 11;
}
rule void PrintStation()
{
variable integer I;
for I : = 1 to WMain.itemcount[ .channel ]
do
print WMain.channel[ WMain:index(.channel, I ) ];
endfor
}
In row 11 the loop is run through from 1 up to the number of items according to the internal structure. In row 13 the index is calculated (WMain:index(.channel, I )) which indexes the associative array .channel.
2.5.2 Method delete
To delete any number of items, the user may apply the method :delete().
<Object>:delete( attribute <Name>, anyvalue <Index>);
In this case <Object> will be the object, having defined the array <Name>. <Index> indicates the item to be deleted.
Example
window WMain
{
integer channel [ string ];
.channel [ "MTV" ] := 65;
.channel [ "CNN" ] := 14;
.channel [ "NBC" ] := 11;
}
rule void DeleteStation()
{
variable integer I;
for I : = WMain.itemcount[ .channel ] to 1 step -1
do
WMain:delete( .channel, WMain:index(.channel, I ) );
endfor
}
!! Second, more elegantly programmed deletion routine
rule void DeleteStation_2()
{
while (WMain.itemcount > 0)
do
WMain:delete( .channel, WMain:index(.channel, 1 ) );
end
}
Both rules have an identical function. The first rule makes use of the internal structure and runs through the loop from the start to end. As deletion naturally changes the internal structure the items will be deleted from behind here. As long as there are items available the first item will correspond to the internal structure. The second rule which does always delete the frist item profits from this.