This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v20 R7
Sets
|
Comparison | Current Selection | Sets |
Number per table | 1 | 0 to many |
Sortable | Yes | No |
Can be saved on disk | No | Yes |
RAM per record(in bytes) | Number of | Total number of |
selected records * 4 | records/8 | |
Combinable | No | Yes |
Contains current record | Yes | Yes, as of the time the set was created |
When you create a set, it belongs to the table from which you created it. Set operations can be performed only between sets belonging to the same table.
Sets are independent from the data. This means that after changes are made to a file, a set may no longer be accurate. There are many operations that can cause a set to be inaccurate. For example, if you create a set of all the people from New York City, and then change the data in one of those records to “Boston” the set would not change, because the set is just a representation of a selection of records. Deleting records and replacing them with new ones also changes a set, as well as compacting the data. Sets can be guaranteed to be accurate only as long as the data in the original selection has not been changed.
You can have the following three types of sets:
Notes:
The following table indicates the principles concerning the visibility of sets depending on their scope and where they were created:
Client Process | Other processes on the same client | Other clients | Server process | Other processes on the server | |
Creation in a client process | |||||
$test | X | ||||
test | X | X(Trigger) | |||
<>test | X | X | |||
Creation in a server process | |||||
$test | X | ||||
test | X | ||||
<>test | X | X |
A set can be created inside a transaction. It is possible to create a set of the records created inside a transaction and a set of records created or modified outside of a transaction. When the transaction ends, the set created during the transaction should be cleared, because it may not be an accurate representation of the records, especially if the transaction was canceled.
The following example deletes duplicate records from a table which contains information about people. A For...End for loop moves through all the records, comparing the current record to the previous record. If the name, address, and zip code are the same, then the record is added to a set. At the end of the loop, the set is made the current selection and the (old) current selection is deleted:
CREATE EMPTY SET([People];"Duplicates")
` Create an empty set for duplicate records
ALL RECORDS([People])
` Select all records
` Sort the records by ZIP, address, and name so
` that the duplicates will be next to each other
ORDER BY([People];[People]ZIP;>;[People]Address;>;[People]Name;>)
` Initialize variables that hold the fields from the previous record
$Name:=[People]Name
$Address:=[People]Address
$ZIP:=[People]ZIP
` Go to second record to compare with first
NEXT RECORD([People])
For($i;2;Records in table([People]))
` Loop through records starting at 2
` If the name, address, and ZIP are the same as the
` previous record then it is a duplicate record.
If(([People]Name=$Name) & ([People]Address=$Address) & ([People]ZIP=$ZIP))
` Add current record (the duplicate) to set
ADD TO SET([People];"Duplicates")
Else
` Save this record’s name, address, and ZIP for comparison with the next record
$Name:=[People]Name
$Address:=[People]Address
$ZIP:=[People]ZIP
End if
` Move to the next record
NEXT RECORD([People])
End for
` Use duplicate records that were found
USE SET("Duplicates")
` Delete the duplicate records
DELETE SELECTION([People])
` Remove the set from memory
CLEAR SET("Duplicates")
As an alternative to immediately deleting records at the end of the method, you could display them on screen or print them, so that a more detailed comparison can be made.
4D maintains a system set named UserSet, which automatically stores the most recent selection of records highlighted on screen by the user. Thus, you can display a group of records with MODIFY SELECTION or DISPLAY SELECTION, ask the user to select from among them and turn the results of that manual selection into a selection or into a set that you name.
4D Server: Although its name does not begin with the character "$", the UserSet system set is a client set. So, when using INTERSECTION, UNION and DIFFERENCE, make sure you compare UserSet only to client sets. For more information, please refer to the descriptions of these commands as well as to the 4D Server, Sets and Named Selections section of the 4D Server Reference Manual.
There is only one UserSet for a process. Each table does not have its own UserSet. UserSet becomes “owned” by a table when a selection of records is displayed for the table.
4D manages the UserSet set for list forms displayed in Design mode or using the MODIFY SELECTION or DISPLAY SELECTION commands. However, this mechanism is not active for subforms.
The following method illustrates how you can display records, allow the user to select some of them, and then use UserSet to display the selected records:
` Display all records and allow user to select any number of them.
` Then display this selection by using UserSet to change the current selection.
FORM SET OUTPUT([People];"Display") ` Set the output layout
ALL RECORDS([People]) ` Select all people
ALERT("Press Ctrl or Command and Click to select the people required.")
DISPLAY SELECTION([People]) ` Display the people
USE SET("UserSet") ` Use the people that were selected
ALERT("You chose the following people.")
DISPLAY SELECTION([People]) ` Display the selected people
The APPLY TO SELECTION, DELETE SELECTION, ARRAY TO SELECTION and JSON TO SELECTION commands create a set named LockedSet when used in a multi-processing environment.
Query commands also create a LockedSet system set when they find locked records in the 'query and lock' context (see the SET QUERY AND LOCK command).
LockedSet indicates which records were locked during the execution of the command.
Product: 4D
Theme: Sets
4D Language Reference ( 4D v20 R7)