Structures are variables with members which are referenced by member names. Structures are useful for following pruposes: 1) managing your variables in a structured manner, and 2) providing a form of associative storage. Beyond4P maintains the members in alphabetical order automaatically and uses a very fast bisectional algorithm to find the specified variable. For example, max. 16 internal queries are needed to access a a member with a given member name in a structure containing more than 65,536 members.
Base Variable Name | [ | Member Name | ] |
---|---|---|---|
- String value or expression | - String value or expression | ||
as variable name | as variable member name |
Similar to arrays, existing structures may also be referenced with numeric index values. This is useful when using loops to process
through the structure rapidly. Please note that the structure is always held in an alphabetic order. Negative indexing is also supported, where
-1 refers to the last element (last in alphabetical order), and -2, etc., to the next ones above.
Following approaches are avialable to create structures:
structure...() | Functions like structure() and structure protect() create structures with intial member names and values provided in two sets. |
Direct referencing | Assigning a simple variable with a member name will initialize it with a structure. This will not be possible if the variable is already an array. |
echo("Initialize a structure directly");
a[dog] = Hund;
a[cat] = Katze;
a[gnu] = Gnu;
echo(a[cat]);
see( a[] );
echo(new line, "Initialize with the structure function");
structure( leg count[], { snake, bird, dog, fly, tick }, { 0, 2, 4, 6, 8 } );
see( leg count[] );
Initialize a structure directly
Katze
a[] [Void] (void,full access)
cat Katze (softquoted string,full access)
dog Hund (softquoted string,full access)
gnu Gnu (softquoted string,full access)
Initialize with the structure function
leg count[] [Void] (void,full access)
bird 2 "2" (numeral,full access)
dog 4 "4" (numeral,full access)
fly 6 "6" (numeral,full access)
snake 0 "0" (numeral,full access)
tick 8 "8" (numeral,full access)
Nesting is supported: Every structure element may be a simple variable, a further structure or array of any size.
Since Beyond4P variable structure is a tree with values possible in both root, intermediate and end nodes, the base variables as well as
member variables of those containing sub-members are preserved.
Nested structures:
Base Variable Name | [ | Member Name | , | Member Name | ] |
---|---|---|---|---|---|
- String value or expression | - String value or expression | - § value or expression | |||
as variable name | as variable member name | as variable member name |
echo("Initialize a nested structure containing structure and array");
city[] = Paris;
array( city[district], {ignoe 0, Louvre, Bourse, Temple, Hotel de Ville, etc. } );
city[district,2,area] = 99; // 99 hectars
city[district,2,inhabitants] = 19500;
echo("1st district = ", city[district,1], " last district = ", city[district,-1] );
see( city[] );
Initialize a nested structure containing structure and array
1st district = Louvre last district = etc.
city[] Paris (softquoted string,full access)
district [Void] (void,full access)
Array [ 0] ignoe 0 (softquoted string,full access)
Array [ 1] Louvre (softquoted string,full access)
Array [ 2] Bourse (softquoted string,full access)
area 99 "99" (numeral,full access)
inhabitants 19500 "19500" (numeral,full access)
Array [ 3] Temple (softquoted string,full access)
Array [ 4] Hotel de Ville (softquoted string,full access)
Array [ 5] etc. (softquoted string,full access)
Normally, attempting to access a structure with a non-existing element name will cause an error. Beyond4P supports default members which will be returned in case of no matches. Assign the default value using a blank string ('') as member name and that's it.
echo("Demonstrate default values"); // a[''] is defined below and is used as default variable.
structure( a[], { cow, horse, pig, sheep, '' }, { Kuh, Pferd, Schwein, Schaf, No translation } );
echo( "cow : ", a[horse] );
echo( "goat: ", a[goat] );
Demonstrate default values
cow : Pferd
goat: No translation
Similar to arrays, structure members can also be referenced with index numbers. The only difference is that the members will be rearranged in alphabetical order whenever a new member is added.
names[Mozart] = Wolfgang Amadeus Mozart;
names[Williams] = John Williams;
names[Strauss] = Johan Strauss;
names[Bach] = Johan Sebastian Bach;
names[Beethoven] = Ludwig van Beethoven;
for (i[] = 0, i[] < member count(names[]), i[]+=1)
{
echo(i[], ": ", names[i[]]);
}
echo("names[2] = ", names[2] );
names[Bartok] = Bela Bartok;
echo("names[2] = ", names[2] ); // List of names has shifted down
echo("Last entry is ", names[-1] ); // Negative indexing
0: Johan Sebastian Bach
1: Ludwig van Beethoven
2: Wolfgang Amadeus Mozart
3: Johan Strauss
4: John Williams
names[2] = Wolfgang Amadeus Mozart
names[2] = Ludwig van Beethoven
Last entry is John Williams