For convert x|y|z on one array of 3 elements, two way are simpler: first replace inside the string all | with , and use :toarray
or use a "while" until you do not process the string from the start to the end and create a new string inside the array fore each | and the last at the end of the string.
Something like that, right?
{
:local string "081234567890|West Java|John Due|Another one|Second item"
:local reEncoded
:for i from=0 to=([:len $string] - 1) do={
:local char [:pick $string $i]
:if ($char = "|") do={:set $char ","}
:set reEncoded ($reEncoded . $char)
}
:local test [:toarray $reEncoded]
:put ($test->0)
:put ($test->1)
:put ($test->2)
:put ($test->3)
:put ($test->4)
}
Output:
081234567890
West Java
John Due
Another one
Second item
I have adapted this function that I have found in the Scripting forum and that is valid for what I need, I share it.
:global strEncode do={
:local string $1
:local reEncoded
:local char
:if (([:typeof $string] != "str") or ($string = "")) do={
:return ""
} else={
:for i from=0 to=([:len $string] -1) do={
:set char [:pick $string $i]
:if ($char = "|") do={:set $char ","}
:if ($char = "!") do={:set $char ","}
:set reEncoded ($reEncoded . $char)
}
:return $reEncoded
}
}
Use:
:put [$strEncode "081234567890|West Java|John Due|Another one!Second item"]
Output:
081234567890,West Java,John Due,Another one,Second item