9.10 Split a string into two parts with specified delimiter
Use “?” or “=” as the delimiter to split a string into two.
A website records URLs user access and we want to find the most used search words. Below is part of the website content:
| ID | User | Website |
|---|---|---|
| 1 | Rebecca | https://github.com/search?q=How+to+study+java%3F |
| 2 | Ashley | https://github.com/search?q=report&type=Code |
| 3 | Rachel | https://github.com/search?q=bigdata&type=Repositories |
| 4 | Rachel | https://github.com/search?l=Python&q=bigdata&type=Repositories |
| … | … | … |
In SPL, s.split(d) function works with @1 option to perform a search until the first d is found. This is equivalent to dividing the string into two parts.
SPL script:
| A | |
|---|---|
| 1 | =T(“loginUrls.txt”) |
| 2 | =A1.(Website.split@1(“?”)(2)) |
| 3 | =A2.(~.split(“&”).select@1(like(~,“q=*”))) |
| 4 | =A3.(~.split@1(“=”)(2)) |
| 5 | =A4.group() |
| 6 | =A5.maxp(~.len())(1) |
A1 Import loginUrls table.
A2 The s.split() function uses @1 option to divide A1’s string into two parts.
A3 Split each parameter value with & and select conditions where q=*, which are search words user have used.
A4 Split each search word into two parts by =, and the second segment is the search word.
A5 Group search words.
A6 Get the group with the most members, which is the most frequently used word.
Execution result:
| Value |
|---|
| bigdata |
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/sxd59A8F2W
Youtube 👉 https://www.youtube.com/@esProc_SPL