Step Functions - how to join an array?

0

As of now there is a States.StringSplit($.inputString, $.splitter) intrinsic function to split an string into an array. Is there a way to join an array into an string? Something like States.ArrayJoin($.inputString, $.joiner)? Is there any work around to achieve something like that?

Reason is that I want to replace a character in a string, since there is no replace I thought about splitting and joining which does have the same effect. I'm using an activity and building an url with the taskToken, but unfortunately any "+" character get's replace by a space, so I want to replace it with "%2B".

Jonas
gefragt vor 9 Monaten796 Aufrufe
2 Antworten
1

Yes, but not easily. You can iterate through the array of items you want to join using a Choice that flows to a Pass that uses States.Format to append each array item to a string. It's ugly, but it works.

  1. Create a Pass state that initializes the string accumulator and the number of items in the array, and flow to the Choice state below: Parameters: { "the_string": "", "item_count.$": "$.States.ArrayLength($.my_array)", "current_index": 0, "my_array.$": "$.my_array" }
  2. Create a choice state that tests the array length and current index to see if any items remain to be appended: Choices: [ { "Variable": "$.current_index", "NumericLessThanPath": "$.item_count", "Next": "Append an Item" } ]
  3. Create a Pass state that appends the string and increments the current index, and returns to the Choice state above: Parameters: { "the_string.$": "States.Format('{}your seprator here{}', $.the_string, $. )", "item_count.$": "$.item_count", "current_index.$": "States.MathAdd($.current_index, 1)", "my_array.$":"$.my_array" }

The Choice state will flow to the default Next state once all of the array items have been appended to $.the_string.

Hope that's clear enough.

mlhpdx
beantwortet vor 8 Tagen
0

One option will be to use States.Format() and pass the array contents if the array length is fixed.

AWS
beantwortet vor 9 Monaten
  • Thanks, unfortunately the output of an taskToken does not have an constant number of "+". I already thought about a loop which combines always 2 array values as long as there are more than 1 value in the array. But for an Standard function that's also pretty expensive. Would be nice if there is a native function to achieve that.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen