9.14 Automatically parse one string as appropriate data type

 

Automatically parse a single string as corresponding data type.
According to the Olympic medal table we are trying to find games where China is before Russia in terms of medal count.

Game Nation Medal
30 USA [46,29,29]
30 China [38,27,23]
30 UK [29,17,19]
30 Russia [24,26,32]
30 Korea [13,8,7]

In SPL, parse(s) function parses string s as an appropriate data type.

SPL script:

A
1 =file(“Olympic.csv”).import@cqt()
2 =A1.run(Medal=parse(Medal))
3 =A2.group(Game)
4 =A3.select(~.select(Nation==“China”).Medal>~.select(Nation==“Russia”).Medal)
5 =A4.(Game)

A1 Import Olympic medal ranking table.
A2 Use parse() function to parse Medal field value as a sequence.
A3 Group A2’s records by Game.
A4 Use operator > to compare gold medal count, silver medal count and copper medal count in order, and select games where China’s rank is before Russia’s.
A5 List the eligible games.

Execution result:

Game
23
25
28
29
30