Split the values

Certified Senior Developer

Hi Everyone,

Can anyone please help me how to split values, for example if input is 102-105; 109-111 then output should be 102, 103, 104, 105, 109, 110, 111. 

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Hi! This is definitely something I can help you with. It seems that what you need to do is to return the values at the beginning and end of the ranges given as inputs.

    To start, we would take your beginning input and process it into local variables. I'm guessing your input is either a list of string, like this:

    {
    "102-105",
    "109-111"
    }

    Or it is a single string where the value pairings are concatenated by a semicolon, like this:

    "102-105; 109-111"

    In the first case, the code below should be able to accomplish this:

    a!localVariables(
      local!input: {
        "102-105",
        "109-111"
      },
      /*We begin by iterating over our list of strings.*/
      a!foreach(
        items: local!input,
        /*Next, we want to break apart the given item in the list
        and use it to create a range of numbers that is inclusive
        on both bounds.*/
        expression: a!localVariables(
          /*The split funtion will break apart our input at the given
          character, which in this case is the dash sign. It will return
          a list of all the characters on either side of the dash.*/
          local!splitInput: split(fv!item, "-"),
          /*By indexing into the first item in this list, we will have
          the lower end of the range of numbers to generate.*/
          local!startRange: local!splitInput[1],
          /*The end of that range is the second item in the list*/
          local!endRange: local!splitInput[2],
          /*Finally, we use the enumerate function to generate the
          list of numbers. Because we want to generate a list of numbers
          that begins at our startRange, and ends at our endRange, we 
          only have enumerate however many numbers exist within that range.
          The result of our endRange minus our startRange gives that number
          of numbers, and because enumerate goes from 0 to n-1, we increment
          the range by one. Additionally, the output of enumerate must have
          the start range added to each value in the resulting list, 
          as done below, so that we can have the list start at our range and end
          the appropriate number of numbers after (which will be our endRange!)
          */
          local!startRange + enumerate(
            local!endRange-local!startRange+1
          )
        )
      )
    )

    In our second case, we use effectively the same code, but break apart our inputs differently, by using split() to create a list of strings out of the initial single string input. The code below could be used as the value for the "items" argument in the a!foreach function in the code snippet above to do so.

    split(local!input, ";")

    As a final note, this code will produce a list of lists of numbers. If you need a flat list of just the numbers, I recommend using the flatten function.

Reply
  • 0
    Certified Lead Developer

    Hi! This is definitely something I can help you with. It seems that what you need to do is to return the values at the beginning and end of the ranges given as inputs.

    To start, we would take your beginning input and process it into local variables. I'm guessing your input is either a list of string, like this:

    {
    "102-105",
    "109-111"
    }

    Or it is a single string where the value pairings are concatenated by a semicolon, like this:

    "102-105; 109-111"

    In the first case, the code below should be able to accomplish this:

    a!localVariables(
      local!input: {
        "102-105",
        "109-111"
      },
      /*We begin by iterating over our list of strings.*/
      a!foreach(
        items: local!input,
        /*Next, we want to break apart the given item in the list
        and use it to create a range of numbers that is inclusive
        on both bounds.*/
        expression: a!localVariables(
          /*The split funtion will break apart our input at the given
          character, which in this case is the dash sign. It will return
          a list of all the characters on either side of the dash.*/
          local!splitInput: split(fv!item, "-"),
          /*By indexing into the first item in this list, we will have
          the lower end of the range of numbers to generate.*/
          local!startRange: local!splitInput[1],
          /*The end of that range is the second item in the list*/
          local!endRange: local!splitInput[2],
          /*Finally, we use the enumerate function to generate the
          list of numbers. Because we want to generate a list of numbers
          that begins at our startRange, and ends at our endRange, we 
          only have enumerate however many numbers exist within that range.
          The result of our endRange minus our startRange gives that number
          of numbers, and because enumerate goes from 0 to n-1, we increment
          the range by one. Additionally, the output of enumerate must have
          the start range added to each value in the resulting list, 
          as done below, so that we can have the list start at our range and end
          the appropriate number of numbers after (which will be our endRange!)
          */
          local!startRange + enumerate(
            local!endRange-local!startRange+1
          )
        )
      )
    )

    In our second case, we use effectively the same code, but break apart our inputs differently, by using split() to create a list of strings out of the initial single string input. The code below could be used as the value for the "items" argument in the a!foreach function in the code snippet above to do so.

    split(local!input, ";")

    As a final note, this code will produce a list of lists of numbers. If you need a flat list of just the numbers, I recommend using the flatten function.

Children