Author Topic: Access array in IRD  (Read 3407 times)

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline superman

  • Newbie
  • *
  • Posts: 21
  • Karma: 0
Access array in IRD
« on: November 02, 2019, 12:18:30 AM »
Hello,

I am trying to access an array inside a routing strategy in IRD.
Does anyone have an idea how to access its elements?

Thanks

Offline terry

  • Sr. Member
  • ****
  • Posts: 324
  • Karma: 35
Re: Access array in IRD
« Reply #1 on: November 04, 2019, 09:23:00 AM »
To access array elements the array itself is needed in first place.
How exactly you get the array (or whatever array-like data structure)?

Offline superman

  • Newbie
  • *
  • Posts: 21
  • Karma: 0
Re: Access array in IRD
« Reply #2 on: November 05, 2019, 03:26:23 AM »
Hello Terry, thank you for your response.

I create my array using a macro and assigning the data returned to a variable inside my strategy. I need to access its elements in IRD blocks like targets, prompts etc by calling something like myArray[1].

Thank you!

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7623
  • Karma: 56330
Re: Access array in IRD
« Reply #3 on: November 05, 2019, 06:12:15 AM »
But what data type you used? List? Show an URS sample of your data

Offline terry

  • Sr. Member
  • ****
  • Posts: 324
  • Karma: 35
Re: Access array in IRD
« Reply #4 on: November 05, 2019, 08:02:33 AM »
Basically as Cavagnaro mentioned it should depend from what variable type is used on strategy side.

 Plus if you using Ecma Script macros (as you created array probably you do) inside regular IRD strategies (which are written on other language) you effectively mixing 2 languages and in such case using macro is implemented by calling subroutine mechanism (macro is turned into independent subroutine). As result passing data back from macro to strategy is not so straightforward - they do not share variables (if not count INTERACTION scoped ones). Will be good to know how exactly you passing data from macro back to strategy.

And very likely attempt to use array in regular IRD strategies will result in approach like turning array into JSON string (on Ecma Script side), passing resulted string back to IRD strategy, turning it into LIST (often can be done just by assigning this string to LIST type variable) and after that using KVList functions like KVListGetStringValueByInd[list, index] to get elements of the list.

Offline superman

  • Newbie
  • *
  • Posts: 21
  • Karma: 0
Re: Access array in IRD
« Reply #5 on: November 07, 2019, 02:59:45 AM »
Basically I have used multiple ways to access the data but I was trying to find the most effective one.
The issue is with the data returned to the strategy as you mentioned. I use ReturnData[] function and assign the data to the list variable which I then access through the functions you mentioned. But, the issue is that I most probably want a json object and not an array. However, I haven't found an optimal way to parse the json object in the strategy.

Offline Janis

  • Full Member
  • ***
  • Posts: 123
  • Karma: 4

Marked as best answer by superman on November 09, 2019, 06:07:57 PM

Offline terry

  • Sr. Member
  • ****
  • Posts: 324
  • Karma: 35
Re: Access array in IRD
« Reply #7 on: November 07, 2019, 07:17:56 AM »
I don't think parsing of JSON strings manually is a good idea, better to make URS to make parsing/conversions "automatically" - convert it into KVList object  (and access it with functions KVListGet…)or to convert it to string in format key:value|key:value|.... and either manually parse it or access its elements with functions GetStringKey, ListGetString, etc. 

1. If in macro code array is some Array and there is construction ReturnEx(0. array); then array will be serialized into string (as ReturnEx has type of second parameter STRING) and this string will be returned with ReturnData[] function in strategy. If array is something like ["abc", "cde", 123] then as serialized STRING it will be "#0:abc|#1:cde|#2:123". When serialized to string arrays/objects/KVLists by default has format key:value|key:value|...." (in case of array they keys have form like #num). You can either use list manipulation functions to get elements of such strings or you can parse such string (as its format is quite simple).

2. If in macro you pass back JSON presentation of array ReturnEx(0. JSON.strigify(array)) then what will be passed will be actual JSON string '["abc", "cde", 123].
After getting with ReturnData in strategy it can parsed/converted into KVList object (if LIST var= ReturnData[]) and after that elements of list objects can be accessed with LIST manipulation functions (KVListGet...). If needed the LIST value can be reassigned to some STRING variable and as result converted back to string but now it will be not JSON format but default one key:value|key:value|..... and see item 1.