Example Structured Text calculated (a)

Structured Text is a very common file format, the calculation of structured text is also very common requirement. In implementing this calculation, an easy way is to think of a file into a database and then calculate, but it is time consuming and expensive database resources, and under some occasions and no suitable database is available. As a result, we will have a natural idea, if we can direct calculation would be much more convenient. Unfortunately, the general high-level languages ​​do not provide for the basic operation library is structured text, and want to complete these complicated and very hard-coded operation, not only code complexity, maintainability is also very poor.

As a professional library of structured data calculation, calculator collector encapsulated SPL rich structural calculation function, support set operation, association operations, orderly operation, operation can be easily achieved thus structured text. In addition, SPL can also call the JDBC interface provides the operation results in a Java application (refer to [ Java script how to call SPL ]), which greatly facilitates integration.

Here we look at the common case of structured text computing, as well as the corresponding SPL solution.


maintain

Record increase

         Inserting a record of row 2 sales.txt. Source files are as follows:

OrderID Client SellerId Amount OrderDate
26 IT 1 2142.4 2009-08-05
33 DSGC 1 613.2 2009-08-14
84 GC 1 88.5 2009-10-16
133 HU 1 1419.8 2010-12-12
32 JFS 3 468 2009-08-13
39 NO 3 3016 2010-08-21
43 KT 3 2169 2009-08-27

         Code


A
1 =file("D:\\sales.txt").import@t()
2 =A1.insert(2,200,"MS",20,2000,date("2015-02-02"))
3 =file("D:\\sales.txt").export@t(A1)

         

result    

OrderID Client SellerId Amount OrderDate
26 IT 1 2142.4 2009-08-05
200 MS 20 2000 2015-02-02
33 DSGC 1 613.2 2009-08-14
84 GC 1 88.5 2009-10-16
133 HU 1 1419.8 2010-12-12
32 JFS 3 468 2009-08-13
39 NO 3 3016 2010-08-21
43 KT 3 2169 2009-08-27

Insert Insert recording function, the first parameter is the insertion position, when the parameter is 0, the additional recording.

         If only append records to the file, not reading the file, simply use the function export @ a, code is as follows:


A
1 =create(OrderID,Client,SellerId,Amount,OrderDate).record([200,"MS",20,2000,date("2015-02-02")])
2 =file("D:\\sales.txt").export@a(A1)

New functions create a two-dimensional table, the recording function to record an additional two-dimensional table.

Further, the recording can be inserted through the bulk insert @ r, code is as follows:


A
1 =file("D:\\sales.txt").import@t()
2 =create(OrderID,Client,SellerId,Amount,OrderDate)
3

=A2.record([200,"MS",20,2000,date("2015-02-02"),

300,"Ora",30,3000,date("2015-03-03")])

4 =A1.insert@r(2:A2)

         For the sake of brevity, we will hereinafter be omitted code files exported.

Delete Record

         The deletion of article 2 of the record sales.txt. code show as below:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.delete(2)

         Support batch delete function to delete, such as deleting the first 2,3,5,6,7 record: A1.delete ([2,3] | to (5,7))

         It may be conditionally deleted, such as deleting records Amount less than 1000: A1.delete (A1.select (Amount <1000))

Modify records

         Review Article sales.txt recording, the SellerId to 100, Amount to 1000, as follows:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.modify(2,100:SellerId,1000:Amount)

         也可以批量修改,比如将前10条记录的Amount增加10:

         A1.modify(1:10,Amount+10:Amount)

增加列

         在sales.txt增加列year,填入订单日期OrerDate中的年份。代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.derive(year(OrderDate):year)

         结果:

1559539163195100.png

删除列

         物理上删除列效率较低,通常用“取出保留列”来代替。比如sales.txt中删除Client、SellerId,相当于保留OrderID、Amount、OrderDate,代码如下:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.new(OrderID,Amount,OrderDate)

         结果:

1559539163275100.png

修改列

         将sales.txt的Amount列增加10%,代码如下:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.run(Amount*1.1:Amount)

结果:

1559539163336100.png

         注意函数run跟函数modify的区别:修改整列(所有记录的对应字段)需要用run,只修改指定记录的某列(特定字段)用modify。

 

基本运算

查询

         指定时间段,按参数查询sales.txt。代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.select(OrderDate>=startDate   && OrderDate<=endDate)

         startDate和endDate是输入参数,比如2010-01-01至2010-12-31。结果:

1559539162632100.png

排序

针对sales.txt,按照客户代码(Client)降序排序,按照订单日期(OrderDate)升序排序。

代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.sort(-Client,OrderDate)

注意:降序时在字段前面使用英文的减号来表示,即“-”,默认按照升序。

结果:

1559539162739100.png

分组汇总

计算出每个销售员每年的销售额和订单数,即按照销售员分组,对销售额求和,对记录计数。

代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.groups(SellerId,year(OrderDate);sum(Amount),count(~))

函数groups可在分组的同时进行汇总,其中,~表示每组或当前组,count(~)等于count(OrderID)。

结果:

1559539162825100.png

获得唯一值

列出sales.txt中的客户名单,即获取所有Client的唯一值。

代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.id(Client)

结果:

1559539162933100.png

去除重复

保留sales.txt中每个客户每个销售员的第一条记录。获取唯一值也是一种去重,这里是另外一种通过分组来去除重复的方式。

代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.group@1(Client,SellerId)

通过函数group进行分组(和groups不同,这里可以不汇总),@1表示取每组第1条记录。

结果:

1559539163026100.png

 

TopN

         找到每个销售员销售额最大的3笔订单。

代码:


A
1 =file("D:\\sales.txt").import@t()
2 =A1.group(SellerId;~.top(3;-Amount):t).conj(t)

函数top过滤出TopN,”-”表示逆序,函数conj用于合并结果。

计算结果:

1559539163107100.png

如果只取最大的一笔订单,还可以用maxp函数,不过 maxp直接返回表达式描述的最大记录,因此不用再加符号”-”来描述排序方式。由于分组后的字段t的内容是记录,因此不能用conj(t)来合并,而是需要使用A.(t)方式直接取出t字段。所以取每个销售的最大一笔订单表达式为:=A1.group(SellerId;~.maxp(Amount):t).(t)


Guess you like

Origin blog.51cto.com/12749034/2438117