| Dictionary |
|
typedict |
| |
A
Dictionary
is a heterogeneous collection of name/value pairs.
Elements in a dictionary are often referenced using familiar dot notation,
dict.x dict.$magic dict.a_longer_name
where any name the parser accepts is allowed.
Subscript and pointer notation also work,
dict["x"] dict[val] dict["Times-Roman"] dict[3] *(dict + 3)
so run-time evaluation and arbitrary names are also supported.
The subscript itself (i.e., the expression in brackets),
must evaluate to a string, which is used as the name for a lookup,
or an integer, which is used as an index into the dictionary.
Dictionary elements are uninitialized until they are assigned a value,
either by an initializer in a declaration or by an explicit assignment
statement.
The first value assigned to a dictionary element determines its type.
Try to use an uninitialized element and you will get an
undefined
error.
Dictionaries are hashed by name, so indexing through a dictionary
enumerates the elements in an implementation dependent order.
Dump a dictionary with
toString
or use the
%O
format with
printf,
and the elements are automatically sorted by name, which means position
in the dump has nothing to do with where the elements are stored.
| |
| Example: |
The program,
import yoix.stdio.*;
Dictionary d1[3];
d1.magic = "xyzzy";
Dictionary d2[5] = {
int counter = 0;
String greeting = "hello";
double time;
};
Dictionary d3 = {
int n = 1;
int m = 2;
Dictionary dd = {
double x = 12.5;
double y = 109.5;
};
Object obj;
};
printf("d1=%O\nd2=%O\nd3=%.2O\n", d1, d2, d3);
creates three dictionaries. initializes some of the elements, and then prints
d1=Dictionary[3:0]
>magic=^"xyzzy"
--uninitialized--
--uninitialized--
d2=Dictionary[5:0]
counter=0
greeting=^"hello"
time=0.0
--uninitialized--
>--uninitialized--
d3=Dictionary[4:0]
>dd=Dictionary[2:0]
>x=12.5
y=109.5
m=2
n=1
obj=NULL:POINTER
on standard output.
Notice how we used the
%O
format to get a quick dump of the dictionaries and added precision to
the format to get a full dump of the last dictionary.
| | |
| See Also: |
Array,
Hashtable,
String,
Vector
|
|
Yoix is a registered trademark of AT&T Inc.
|