Count by Group

Question

I have a text file with content like this and having 792 lines:

der 17788648

und 14355959

die 10939606

Die 10480597

 

Now I want to compare if "Die" and "die" are equal in lowercase. So if two strings in lowercase are equal, copy the word into a new text file in lowercase and sum the values.

Expected output:

der 17788648

und 14355959

die 21420203

 

I have this so far:

 

  try {

        BufferedReader bk = null;

        BufferedWriter bw = null;

 

        bk = new BufferedReader(new FileReader("outagain.txt"));

        bw = new BufferedWriter(new FileWriter("outagain5.txt"));

 

        List<String> list = new ArrayList<>();

        String s = "";

        while (s != null) {

            s = bk.readLine();

            list.add(s);

        }

 

 

        for (int k = 0; k < 793; k++) {

            String u = bk.readLine();

            if (list.contains(u.toLowerCase())) {

 

                //sum values?

 

            } else {

                bw.write(u + "\n");

            }

        }

 

        System.out.println(list.size());

 

    } catch (Exception e) {

        System.out.println("Exception caught :" + e);

}

 

Answer

This is basic grouping and aggregation operation. As Java lacks a universal structured computation class library, the hardcoding is roundabout. Here we handle it in SPL (Structured Process Language):

A

1

=file("d:\\data.txt").import(;," ").groups(lower(#1);sum(#2))

2

=file("d:\\result.txt").export(A1;   " ")

A1: Group the txt file by lowercase in the first field and then sum the second field.

A2: Use this statement if you want to write the result to a target file.

To integrate an SPL script with a Java application, refer to How to Call an SPL Script in Java