0

Treating string with commas, as an array?

Hi,

I have a string in a text field, lets say it is 'colours' : red, blue, black.

If I use item(colours, 3), it will return 'e', instead of black, due to it being a string and not an array.

Is there a way to treat a string with comma seperated values as an array to select items?

My aim is to use a button to create a record for each 'item', and so the next step I need to know after this is how to loop the creation for as many times as necessary until all item records have been created

4 replies

null
    • Sean
    • 4 yrs ago
    • Reported - view

    You can use the split() function to convert a string to an array. Following your example it formula would be...

     

    split(colours, ", ")

     

    You can use the count() function to get the number of items and the item() function to select a specific item. I would use a for-loop to create the records.

    • Richard_Bramall
    • 4 yrs ago
    • Reported - view

    Perfect, thanks Sean, that has solved this for me.

    I am unfortunately not too familiar with the loop function, struggling to get my head around it but I'm sure if it can be put into context of this scenario it will help me greatly going forwards!

    My formula as it stands is :

    let thistimesheet := Id;
    let newitem := (create 'items test');
    let secondfixitems := split('Heating Second Fix', ",");
    let secondfixquantity := split('Heating Second Fix Quantity', ",");
    let secondfixprices := split('Heating Second Fix Prices', ",");
    let countofitems := count(secondfixquantity);
    newitem.(test := thistimesheet);
    newitem.(item := thistimesheet.item(secondfixitems, 2));
    newitem.(quantity := thistimesheet.item(secondfixquantity, 2));
    newitem.(price := thistimesheet.item(secondfixprices, 2))

    So, the above at the moment is to achieve as follows:

    3 seperate text fields with arrays of item, quanity and price, the button creates a seperate record for each item in the array seperating out to fields item, quantity and price in the new record. As you can see this will work once, for whichever item I choose, above it is just doing it on item 2. How would I get it to loop through all the items?

     

    Thanks

    • Sean
    • 4 yrs ago
    • Reported - view

    Richard, I can't test this, but I'm pretty sure this will work...

     

    let thistimesheet := Id;
    let secondfixitems := split('Heating Second Fix', ",");
    let secondfixquantity := split('Heating Second Fix Quantity', ",");
    let secondfixprices := split('Heating Second Fix Prices', ",");
    let countofitems := count(secondfixquantity);
    for i in range(0, countofitems) do
    let newitem := (create 'items test');
    newitem.(test := thistimesheet);
    newitem.(item := thistimesheet.item(secondfixitems, i));
    newitem.(quantity := thistimesheet.item(secondfixquantity, i));
    newitem.(price := thistimesheet.item(secondfixprices, i))
    end

    • Richard_Bramall
    • 4 yrs ago
    • Reported - view

    Again, worked perfectly with no changes my end, and the loop function makes sense to me now I think.

    Thanks Sean, greatly appreciated!

Content aside

  • 4 yrs agoLast active
  • 4Replies
  • 2131Views