Multiline Group

Question

I need help in getting data from an order form and then putting it into a CSV.

Example order form:

 

**

 

First Name:John

 

Last Name:Smith

 

Middle Name:Lucy

 

**

 

I need to make a script that lets me change the order form to a CSV that has the order details in order. I'm thinking about using Python, but Hava is my strong point.

Example CSV:

John,Smith,Lucy

 

Answer

In the original file, lines are separated by two asterisks (**). What you need is to join non-empty lines into one row. It’s complicated to read-in data and process it by loop in Python. And, it’s impossible to integrate a Python script into a Java application. Here I try handling it in SPL (Structure Process Language). The coding is much simpler:

A

1

=file("d:\\OrderForm.txt").read@n()

2

=A1.group@o(~=="**")

3

=A2.(~.regex(".*:(.*)").(#1))

4

=A3.select(~!=[])

5

=A4.(~.concat@c())

6

=file("d:\\result.csv").write(A5)

A1: Read in each line of OrderForm.txt and return it as a string; each line is a member of the resulting sequence.

 undefined

A2: Compare each line with its next one to group these lines according to whether the string is **.

 undefined

A3: Match the string members in A2’s sequence with a regular expression.

 undefined

A4: Get non-empty sequences.

 undefined

A5: Join up A4’s members into a string by comma.

 undefined

A6: Write A5’s result into result.csv.

An SPL script is easily integrated with a Java application. See How to Call an SPL Script in Java to learn more.