15. Displaying data in a dynamic number of column groups
14. Computations according to intervals
RegisterSiteA.csv stores large amount of exam registration information. Find applicants who registered on December 1, and display their IDs and Names in the specified column groups (specified by variable cols) in a row. For example, if cols=3, each row will record information of three applicants in columns ID1, NAME1, ID2, NAME2, ID3 and NAME3.
Expected result:
SPL code:
A | B | C | |
---|---|---|---|
1 | =file(“RegisterSiteA.csv”).cursor@ct(ID, NAME) | 3 | =B1*100 |
2 | =B1.([“ID”/#,“NAME”/#]).conj() | =create(${A2.concat@c()}) | =B1.(“B4(”/#/“).m(#B5).ID,B4(”/#/“).m(#B5).NAME”).concat@c() |
3 | =A1.select(string(REGTIME, “MMdd”)==“1201”) | ||
4 | for A3,C1 | =B1.(A4.step(B1,#)) | |
5 | for B4(1) | >B2.insert(0,${C2}) |
The variable specifying how many column groups there will be is directly typed in B1, and can be changed as needed. As the volume of data involved is large, A1 defines a file cursor. C1 specifies the number of records to be retrieved at one time. A2 generates column headers of the result table according to the number of columns; it uses string concatenation operator / to join up string and numeric value as a string. Below is A2’s result:
B2 creates a new table sequence based on A2. It uses the macro, which concaenates the string generated in A2 into an expression and then parse and execute it, because column names are not fixed. Similarly, this issue of non-fixed column names also needs to be taken care of. C2 prepares expressions needed for populating data in advance, where #B5 represents sequence number of the member over which B5 loops. Besides, sometimes the data cannot fill all cells within the column groups, so B4 uses A.m(n) instead of A(n) to prevent parameter n from overrun.
A3 selects records containing the target date from A1’s cursor. It uses string(dt, ftm) function to directly obtain the month and day part from the registration time and judge whether they are the specified date. A4 loops A3’s cursor, each time retrieves the number of records specified in C1 from A3’s cursor. B4 uses step() function to arrange data A4’s result table in a number of column groups specied in B1. B5 loops through data in the first column group. C5 uses the macro to write the insert expression and populates data in the current column group to B2’s table sequence.
Execute the code and we can get the final result set in B2. Here we assume that the data in column groups can be wholly stored in an in-memory table sequence. If the result set is too large to fit into the memory, we need to use f.write@a() function in A4’s loop code block to write it to a file.
16. Using JSON strings
Contents and Exercise Data
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/2bkGwqTj
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version