Magic,
Why there are so many output fields?
To get corresponding values, when POSNR does not start with "0". If we do not use all output variable, we will get wrong values. The code is sample, you can edit as per the actual requirement.
I suggest to implement the "grouping part" in the blog mentioned above in sandbox. You will get better idea about this issue.
Sample code: -
public void udf_getSeg(String[] POSNER, String[] VGPOS, String[] HIPOS, String[] LFIMG,ResultList Segment_out, ResultList POSNR_out, ResultList VGPOS_out, ResultList HIPOS_out, ResultList LFIMG_out, Container container) throws StreamTransformationException { for (int i = 0; i < POSNER.length; i++) { if (POSNER[i].startsWith("0")) { Segment_out.addValue(""); POSNR_out.addValue(POSNER[i]); POSNR_out.addContextChange(); VGPOS_out.addValue(VGPOS[i]); VGPOS_out.addContextChange(); HIPOS_out.addValue(HIPOS[i]); HIPOS_out.addContextChange(); int sum = 0; for (int j = 0; j < VGPOS.length; j++) { if (VGPOS[i].equals(VGPOS[j])) { sum = sum + Integer.parseInt(LFIMG[j]); } } LFIMG_out.addValue(sum); LFIMG_out.addContextChange(); } } }