array to structure ...

Prev Next

Function Names

array to structure, array to array structure

Description

This function converts an array to a structure. This is done by adding the member names. While array to structure affects the direct variable members only, array to structure recursive will also convert all nested members (grandchildren, etc). Multiple variables can be converted with one single function call by providing all of them as function parameters. No actions are taken if the variable provided is a simple variable or contains arrays only.

Note: No protection may be applied on the variable, incl. prevent deleting, otherwise exceptions are asserted.

If only the 1st parameter is specified, then the member names are ascending numbers (beginning with 0) with 8 digits and leading zeros. This ensures that the members being converted are still retained in alphabetic order ("00000000", "00000001", "00000002", etc.) as long the number of members is less than 1 billion.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

1, 2

Parameters

No.TypeDescription
1, etc.
code
variable
:string
Variable name

The structure members of this variable will be converted to to array members.

Opt. 2
input
numeral number of digits

The number of digits can be changed to a different value other than 8. Values below 1 are considered as 1 digit.

Default value: 8
Alt. 2
input
string Member name prefix

The member name will be placed before the number. Example: "Nr. " -> "Nr. 0", "Nr. 1", etc.

Alt. 2
input
parameter set Member names

Alternatively, a parameter set can be provided with member names to be applied sequentially. The contents of every parameter set element will be converted to strings first. Nested parameter elements will be treated as strings, i.e. 2nd element in member name in {a,{b,c},...} will actually be called "{'b','c'}". If the parameter set contains more elements than members to be named, then the excessive elements will be ignored. If fewer elements are provided, then the remaining members will be numbered with 8-digit numbers (with leading zeros) as if no parameter would be provided.

Example: {true, a, date("31.12.2015"), 123 } applied on an array with 6 members will convert to "true", "a", "2015-12-31", "123"} and then sorted to "123", "2015-12-31", "a", "true". Duplicate member names will be resolved with automatic addition of numbers, E.g. { a, a, a } would result in names like { a, a_1, a_2 } using underscore symbols.

Examples

  array( a[], { a, b, c, { d, e }, f, g, h } );
  see( a[] );

  b[] <== a[];
  array to structure( b[] );
  echo("Conversion with 1 paramemter only:");
  see( b[] );


  b[] <== a[];
  array to structure recursive( b[], 3);
  echo("Conversion with number in 2nd parameter: 3 digits ");
  see( b[] );

  b[] <== a[];
  array to structure recursive( b[], "No. " );
  echo("Conversion with string in 2nd parameter:");
  see( b[] );

  b[] <== a[];
  array to structure recursive( b[], {A,B,C,F} );
  echo("Conversion with parameter set in 2nd parameter:");
  see( b[] );

  b[] <== a[];
  array to structure ( b[], {true, a, date("31.12.2015"), 123 } );
  echo("Conversion with diverse contents in parameter set in 2nd parameter:");
  see( b[] );

Output

a[]                     [Void]                     (void,full access)
Array [   0]            a                          (softquoted string,full access)
Array [   1]            b                          (softquoted string,full access)
Array [   2]            c                          (softquoted string,full access)
  Array [   0]          d                          (softquoted string,full access)
  Array [   1]          e                          (softquoted string,full access)
Array [   3]            f                          (softquoted string,full access)
Array [   4]            g                          (softquoted string,full access)
Array [   5]            h                          (softquoted string,full access)

Conversion with 1 paramemter only:
b[]                     [Void]                     (void,full access)
00000000                a                          (softquoted string,full access)
00000001                b                          (softquoted string,full access)
00000002                c                          (softquoted string,full access)
  Array [   0]          d                          (softquoted string,full access)
  Array [   1]          e                          (softquoted string,full access)
00000003                f                          (softquoted string,full access)
00000004                g                          (softquoted string,full access)
00000005                h                          (softquoted string,full access)

Conversion with number in 2nd parameter: 3 digits
b[]                     [Void]                     (void,full access)
000                     a                          (softquoted string,full access)
001                     b                          (softquoted string,full access)
002                     c                          (softquoted string,full access)
  000                   d                          (softquoted string,full access)
  001                   e                          (softquoted string,full access)
003                     f                          (softquoted string,full access)
004                     g                          (softquoted string,full access)
005                     h                          (softquoted string,full access)

Conversion with string in 2nd parameter:
b[]                     [Void]                     (void,full access)
No. 0                   a                          (softquoted string,full access)
No. 1                   b                          (softquoted string,full access)
No. 2                   c                          (softquoted string,full access)
  No. 0                 d                          (softquoted string,full access)
  No. 1                 e                          (softquoted string,full access)
No. 3                   f                          (softquoted string,full access)
No. 4                   g                          (softquoted string,full access)
No. 5                   h                          (softquoted string,full access)

Conversion with parameter set in 2nd parameter:
b[]                     [Void]                     (void,full access)
00000004                g                          (softquoted string,full access)
00000005                h                          (softquoted string,full access)
A                       a                          (softquoted string,full access)
B                       b                          (softquoted string,full access)
C                       c                          (softquoted string,full access)
  A                     d                          (softquoted string,full access)
  B                     e                          (softquoted string,full access)
F                       f                          (softquoted string,full access)

Conversion with diverse contents in parameter set in 2nd parameter:
b[]                     [Void]                     (void,full access)
00000004                g                          (softquoted string,full access)
00000005                h                          (softquoted string,full access)
123                     f                          (softquoted string,full access)
2015-12-31              c                          (softquoted string,full access)
  Array [   0]          d                          (softquoted string,full access)
  Array [   1]          e                          (softquoted string,full access)
a                       b                          (softquoted string,full access)
true                    a                          (softquoted string,full access)

Try it yourself: Open LIB_Function_array_to_structure.b4p in B4P_Examples.zip. Decompress before use.

See also

structure to array