We have a non-standard format csv file. Odd-numbered lines are records of the primary table – one line corresponding to one record, which consists of 3 fields: idUniversity, nameOfUniversity and noOfBuses, where the 3rd field is a redundant one for recording the number of corresponding records in the subtable represented by the even-numbered line. The even-numbered lines represent records of the subtable; each line has N items, and every two items correspond to one record, which has two fields: idBus and noOfBus.
Task: Use Java to parse the primary-sub tables as a structure that is convenient for subsequent computations.
A1: Parse the csv file as a two-dimensional sequence separated by commas.
A2: Group the sequence every two rows.
A3: Parse the 1st row of each group as the primary table record, and parse the 2nd row as multiple subtable records and use them as the primary table fields.
The above SPL code establishes primary-sub table association, and we can use the dot to access the subtable. To access the subtable record whose idBus is greater than 2 in the 2nd primary table record, we can directly write the code as =A3(4).noOfBuses.select(idBus>2).
Read How to Call a SPL Script in Java to find how to integrate SPL into a Java application.
This is one of the problems on StackOverflow. You can click on it to see that the conventional solution is quite complicated, but the SPL approach is really simple and efficient.
Top comments (0)