Hi All I have requirement to get the values between square brackets iam achieving it with the help of extract function but when i was trying to insert the brackets after entering value and if my text value has uneven number of brackets it is erroring out
can you suggest is their any other alternate way to achieve this
Discussion posts and replies are publicly visible
I would encourage you to validate the incoming string before you attempt to use the extract() function on it. There are probably a few ways to do this but in essence if you check that the string you want to process contains an even number of "[" and "]" characters then you'll avoid the error. What might be a bit harder to achieve is to ensure that every "[" has a following "]" without another intervening "["...if that even matters to you? But the principle here: don't attempt to process data that is effectively invalid.
...of course if you treat "[" and "]" as just pure delimiters then you can do something like this, which isn't sensitive to the need for each "[" to have a matching "]"
fn!reject( a!isNullOrEmpty, a!flatten( a!forEach( items: split(ri!myString, "["), expression: split(fv!item, "]") ) ) )
Is there any function to count the number of "["
will definetly try this Thank you for the quick response
you'll want to check out standard appian expression functions like "find()" to help with that.
edit: find() doesn't actually work for this (grumble) but i believe my example below will.
Not directly, no. You have to break up the string into an array containing its individual characters and then you can count. Here's something that I played with:
a!localVariables( local!stringAsArray: fn!char(fn!code(ri!myString)), local!scoring: a!forEach( items: local!stringAsArray, expression: a!match( value: fv!item, equals: "[", then: 1, equals: "]", then: - 1, default: 0 ) ), local!scoreSum: fn!sum(local!scoring), local!statusCode: if(local!scoreSum = 0, 1, 0), local!result: if( local!scoreSum = 0, fn!extract(ri!myString, "[", "]"), "INPUT ERROR" ), a!map( statusCode: local!statusCode, result: local!result ) )
Explanation:
Hope this makes sense
Um...find() only returns the first instance. Frustratingly (since it's such a common use case) there's no function that I'm aware of to count the instances of a string within a string, hence the heavyweight example I've provided.
yeah, you're right - i realized right after.
so i guess we have to resort to a simple workaround like len(cleanwith(local!myString, "[")) - which will extract only that character from the original string then return the length of the result.
i went through all the functions but couldnt find the exact function to count number of"["
There isn't. So you have to get creative. See above responses.