Order-based Calculations: Grouping & String Concatenation

Question

I want to read in data from a text file using Java. Each time id1 value or id2 value changes, I need to insert END, START (Details are listed below) in id3 column after the current value and output the new id3. I’m new to Java. I can read in the text and write the new text but I don’t know how to compare values in id1 and id2. Can anyone help me getting this done? Explanations are appreciated!

Source file:

id1 id2 id3

100037 1 Q

100037 1 SR

100037 1 Q

100037 1 SR

100039 1 Q

100039 1 SR

100044 1 Q

100044 1 SR

100048 1 Q

100048 1 Q

100048 1 SR

100048 2 Q

100048 2 Q

100048 2 SR

100078 1 Q

100078 1 SH

100078 1 SR

100078 1 Q

 

Expected result:

START

Q

SR

Q

SR

END

START

Q

SR

END

START

Q

SR

END

START

Q

Q

SR

END

START

Q

Q

SR

END

START

Q

SH

SR

Q

END

 

Answer

Hardcoding in Java is complicated. It’s simple to code it in SPL (Structured Process Language). Only a 3-liner is enough:

A

1

=file("E:\\source.txt").import@t()

2

=A1.group@o(id1,id2).conj("START"|~.(id3)|"END")

3

=file("E:\\result.txt").export(A2)

A1: Read in content from source.txt.

A2: Group records by id1 and id2 and precede records in each group with START and follow them with END.

A3: Output A2’s result to result.txt.

An SPL script is easily integrated with a Java program. See detailed explanations in How to Call an SPL Script in Java.