Delimeter in Mapper

I have the input value stored in one string separated by delimiter comma. This value is available as Mapper variable.

For Example:

$inputstring - Value1,Value2,Value3,Value4,Value5
i have to separate these values and store separately in diff variables like mentioned below. Can anyone advise if there is any suitable functionality available for this. Anything in mapper will be helpful

Value1- Variable 1
Value2 - Variable 2
Value3 - Variable 3
Value4 - Variable 4
Value5 - Variable 5

I think you should be able to do a split.

https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439357/String+Functions+and+Properties#StringFunctionsandProperties-split

The result would be an array and from that point you can assign each item in array to each variable.

Hope that helps!

Thanks for your response. It helps.

i separated using split function, but when i was trying to assign them using array for each variable like $myarray.find(x => x[1]), $myarray.find(x => x[2]), $myarray.find(x => x[3]), $myarray.find(x => x[4]) etc, only the first value of the array is assigned to all the variables. The subsequent values which were splitted/separated by comma are not assigned/showing up.

But i was able to use alternate method using substring and were able to assign. Can i know if i am missing something in array.

Try this; I created an pipleine which took your input string, and then I used two mappers (should be able to make that one expression, but for simplicity, I used two). The first mapper takes the string and applies the split method:
$inputstring.split(',’)
And then the second takes the array produced there and extends the original document with the array as fields:
{}.extend($inputarray)
And here’s the output:
14
19 ConvertStringToFields_2019_04_13.slp (6.4 KB)

1 Like

Thank you. It is working now. Good one to learn. :smiley:

I wanted to explain why these expressions weren’t working. The find() method on arrays will iterate through the elements of the array and return the first element where the callback returned true. In this case, the callback supplied is indexing into the string (e.g. x[1] gets the second character in the string) and returning a character. Since a character is “truthy” the callback will always return true for the first element in the array and so that is being returned.

As mentioned in the other answer, you only really needed to add a second mapper where there were mappings that indexed into the array, like so: $myarray[0], $myarray[1], and so on.

1 Like