cancel
Showing results for 
Search instead for 
Did you mean: 

UDF Help

Former Member
0 Kudos

Hi Guys,

Can you please help me with this UDF.

I have a requirement where I need to output the counter where ever the value matches in the queue.

We have to check 1st value of the queue against all the values and so on and if the value matches then we pass the counter/number to the output else suppress the value.

Input

1-Shipping

1-VAT

1-Discount

1-Discount

1-Shipping

1-Discount

Context Change

2-Shipping

2-VAT

2-Discount

2-VAT

But I need the output like this

1

SUPPRESS

1

2

2

3

SUPPRESS

1

SUPPRESS

2

Accepted Solutions (1)

Accepted Solutions (1)

former_member182412
Active Contributor
0 Kudos

Hi Asif,

Use below UDF:

Execution Type: All Values of Context


public void counter(String[] input, ResultList result, Container container) {

  Integer count = 0;

  List<String> list = Arrays.asList(input);

  Map<String, Integer> map = new HashMap<String, Integer>();

  for (String key : input) {

  count = (map.get(key) == null) ? 0 : map.get(key);

  count++;

  map.put(key, count);

  if (Collections.frequency(list, key) > 1)

  result.addValue(count);

  else

  result.addSuppress();

  }

  }

Test:

Regards,

Praveen.

Former Member
0 Kudos

Thank you so much Praveen.

It is working like a Charm!!

Would be great if you could explain your code a bit as I have not seen such an statements before.

  List<String> list = Arrays.asList(input); 

  Map<String, Integer> map = new HashMap<String, Integer>(); 

  for (String key : input) { 

  count = (map.get(key) == null) ? 0 : map.get(key); 

  count++; 

  map.put(key, count); 

  if (Collections.frequency(list, key) > 1) 

former_member182412
Active Contributor
0 Kudos

Hi Asif,

I have added the comments to UDF


public void counter(String[] input, ResultList result, Container container) {

  Integer count = 0;

  //Create array list from array

  List<String> list = Arrays.asList(input);

  //create a map to hold key and value

  Map<String, Integer> map = new HashMap<String, Integer>();

  for (String key : input) {

  //this is equal to if else statement var = (condition)?condition true:condition false

  count = (map.get(key) == null) ? 0 : map.get(key);

  count++;

  map.put(key, count);

  //Collections.frequency method will tell you how many occurrences of the given element

  if (Collections.frequency(list, key) > 1)

  result.addValue(count);

  else

  result.addSuppress();

  }

  }

Regards,

Praveen.

Answers (0)