Extract Function

Certified Associate Developer

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

Parents
  • 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.

  • 0
    Certified Associate Developer
    in reply to Stewart Burchell

    Is there any function to count the number of "[" 

  • 0
    Certified Lead Developer
    in reply to tejashwinin0822

    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:

    1. fn!char(fn!code(ri!myString)) generates the array of individual characters as described
    2. the a!forEach() then assigns a score for each character
    3. we then sum the individual scores - if there are an even number of "[" and "]" the score will be zero
    4. we then return an a!map() with the result. This contains a statusCode to indicate if the original string was valid or not, and a result

    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.

Reply Children