on 09-28-2016 3:41 PM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
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.
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.