& Transactions between Variables

Prev Next

Introduction

Different from transactions without assignment operators, the & assignment operator in front of the transaction symbol will assume the following rules focussing on overlaying data from the source variable in the destination variable:

  • Transactions can be carried out on base variables as well as member variables.
  • If the destination variable does not yet exist, then it will be created as simple variable containing void value before the transaction begins. However, the source variable on the right-hand side must exist.
  • The existing setup of structure and array members and sub-members in the destination variable will not be altered.
    • No further arrays or structures will be added
    • No additional members will be added to existing array and structure members
  • Where possible, all structure and array members and sub-members of the source variable will be included in the transaction.
    • Values in member positions in the source variables for which no corresponding position is existing in the destination variable will not be transferred.
  • The base variable of the specified destination will be overwritten if the ^ suffix is not used.



Following actions are applied on the destination variables using this assignment operator:

Destination Source Description
Simple or zero members Simple or zero members The value of the base variable will be transferred as long no ^ suffix is specified.
Simple or zero members Array Base variable: See above. The array nembers are not transferred. Same applies to their sub-members.
Simple or zero members Structure Base variable: See above. The structure nembers are not transferred. Same applies to their sub-members.
Structure Simple or zero members Base variable: See above.
Structure Array Base variable: See above. The array members are not copied into structure members as they are incompatiable (missing names).
Structure Structure Base variable: See above. The members for which corresponding member positions are already existing in the destination variable will also be transferred. - Existing members will be overwritten
- No additional member names will be added.
Array Simple or zero members Base variable: See above.
Array Array Base variable: See above. The members for which corresponding member positions are already existing in the destination variable will also be transferred. - If the destination has more members than the source, then the excess destination members remain unaffected.
- If the destination has fewer members than the source, then the remaining members in the source variable will not be copied.
Array Structure Base variable: See above. The members for which corresponding member positions are already existing in the destination variable will also be transferred. The existing array members will be replaced by structure members from the source variable, sorted in alphabetic order by member names.
- If the destination has more members than the source, then the excess destination members remain unaffected.
- If the destination has fewer members than the source, then the remaining members in the source variable will not be copied.

Copy Transaction Example
       structure( animals1[], { mammal, bird, fish }, { dog, owl, eel} );
       structure( animals1[mammal], { carnivore, herbivore }, { bear, deer } );

       structure( animals2[], { mammal, fish, reptile }, { cat, trout, turtle } );
       structure( animals2[mammal], { carnivore, omnivores }, { lynx, skunk } );

       animals1[] &<== animals2[];  // Trout and cat overwritten, owl survives
       see(animals1[]);
animals1[]              [Void]                     (void,full access)
bird                    owl                        (softquoted string,full access)
fish                    trout                      (softquoted string,full access)
mammal                  cat                        (softquoted string,full access)
  carnivore             lynx                       (softquoted string,full access)
  herbivore             deer                       (softquoted string,full access)

Try it yourself: Open LAN_Features_AND_Transactions_between_variables.b4p in B4P_Examples.zip. Decompress before use.
Move Transaction Example
       structure( animals1[], { mammal, bird, fish }, { dog, owl, eel} );
       structure( animals1[mammal], { carnivore, herbivore }, { bear, deer } );

       structure( animals2[], { mammal, fish, reptile }, { cat, trout, turtle } );
       structure( animals2[mammal], { carnivore, omnivores }, { lynx, skunk } );

       animals1[mammal] &<<= animals2[mammal];
       // Lynx replaces bear, and the deer continues living.

       see(animals1[]);
       see(animals2[]);
animals1[]              [Void]                     (void,full access)
bird                    owl                        (softquoted string,full access)
fish                    eel                        (softquoted string,full access)
mammal                  cat                        (softquoted string,full access)
  carnivore             lynx                       (softquoted string,full access)
  herbivore             deer                       (softquoted string,full access)

animals2[]              [Void]                     (void,full access)
fish                    trout                      (softquoted string,full access)
reptile                 turtle                     (softquoted string,full access)

Try it yourself: Open LAN_Features_AND_Transactions_between_variables_01.b4p in B4P_Examples.zip. Decompress before use.
Swap Transaction Example
       structure( animals1[], { mammal, bird, fish }, { dog, owl, eel} );
       structure( animals1[mammal], { carnivore, herbivore }, { bear, deer } );

       structure( animals2[], { mammal, fish, reptile }, { cat, trout, turtle } );
       structure( animals2[mammal], { carnivore, omnivores }, { lynx, skunk } );

       animals1[] &<=> animals2[];

       see(animals1[]); // animals1 gets cat, trout, lynx
       see(animals2[]); // animals2 gets dog, eel and bear
animals1[]              [Void]                     (void,full access)
bird                    owl                        (softquoted string,full access)
fish                    trout                      (softquoted string,full access)
mammal                  cat                        (softquoted string,full access)
  carnivore             lynx                       (softquoted string,full access)
  herbivore             deer                       (softquoted string,full access)

animals2[]              [Void]                     (void,full access)
fish                    eel                        (softquoted string,full access)
mammal                  dog                        (softquoted string,full access)
  carnivore             bear                       (softquoted string,full access)
  omnivores             skunk                      (softquoted string,full access)
reptile                 turtle                     (softquoted string,full access)

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