0

I have created an array, using the .Split() functionality, I would like to now reference each item (it I believe) in that array, specifically.

How do I achieve that?


The rest of this you don't need to read, but I'll try to provide more information.

I am now going to use an example of something that I might like to do, this is not literally all I want to do.

So. Let's say a user has input the following data in to the 'Inputta' field:

aaa#bbb#ccc#ddd#eee#fff

Now, in the 'Arraya' field, I have:

Inputta.Split('#')

Let's say I would like to select the "ddd" entry in that array, how would I do that?

I've tried using the .Where() functionality, eg: ".Where(ItemNumber = 4)", but even if that did work (it doesn't), it would be no use for if I need to be iterative.

I've tried to play with the .Select() functionality, but that's not helpful, either, as I can't find decent information on how it even works.


So, on to that iterative tip, to build on this example, I have a secondary requirement of the form, which is to pick an item from the array from an incremented integer.

We'll imagine I know how to increment an integer, and have that information already, either in a separately generated .Split() field, or as an integer calculation in a Repeating Section.

How would I use the integer to find the array entry in the 'Arraya' field?

Basically, how would I select item 1 (from the second array), then item 2, etc?

This isn't a separate question, I'm just explaining further the requirement.


As an addendum, I'd love to know how to create an array of incremental integers leading to a pre-determined max number. ;-)


For the record, I have StartPaged this thoroughly.

  1. I can't find a full reference chart for the functions available anywhere online.
  2. The list on CF only lists a subset of the available functions (for example, the .Select() array functionality is not listed)
  3. Everywhere else appears to handle other specific functions.

None of that is stated to cast any disdain anywhere, purely to indicate that I have looked, perhaps my StartPage Fu is just not that strong. Either way, apologies for asking here.

2 Answers 2

1

.Select() and .Where() are methods written for the repeating sections specifically. The only way to reference elements in an array on Cognito Forms is to use a bracket index.

"aaa#bbb#ccc".Split("#")[0] would give you "aaa"

"aaa#bbb#ccc".Split("#")[1] would give you "bbb"

You can use an integer field instead of writing a number within that bracket.

4
  • Hi, @MarkShamy, I can't remember my original requirement above but am having the same issue manipulating URIs. Already detected and removed the Scheme (eg "http://") in field 'RemoveScheme'. But I think the number you're referencing is a limiter. Using (RemoveScheme.Split('/'))[0] or RemoveScheme.Split('/')[0] in new field 'UriNoPath' works for first entry, but not for any others. Not an issue for that field, but when I do the same with =(UriNoPath.Split('.'))[2] (or 1, or 3) it goes blank. So it seems hard to select an individual item from the array.
    – Eliot
    Jun 29, 2021 at 21:31
  • I can get the UriHost using a few calculation fields (probably mashable into one). So the oddity (which I don't like, because I don't understand it) of field 'UriNoPath' provides "www.extrabit.cognitoforms.com" from URL input. I count the "." in field 'UriComponents' with (UriNoPath.Split('.')).Length (4), and split UriNoPath in field 'UriNoSplit' with UriNoPath.Split(['.'], Int32(UriComponents) - 1). Using field 'UriExtInd' with UriSplit.LastIndexOf(", ") to set the index I set field 'UriHost' with =UriSplit.Substring(Int32(UriExtInd)).Replace(',',' ').Trim(' ').
    – Eliot
    Jun 29, 2021 at 21:56
  • So it stands to reason you could extend that logic to gain access to all parts of the URL array, or just the URI Host. Plus this could be done in one calculation, but it wouldn't be friendly to someone that picks it up later. There are a lot of undocumented functions in usage, and I have had luck using .Select with what I assume is an iterative function .Select(it), but using that to pick a specific (or the first or last) entry has yielded nothing yet. Either way, thank you very much for your answer, as I've found a way to get some stuff, but I don't think the answer is quite right, sorry.
    – Eliot
    Jun 29, 2021 at 22:00
  • Aha! Last does work on the array, here is the syntax: [array].Last(). So, UriHost is actually easier to get with =UriNoPath.Split(['.'], Int32(UriComponents) - 1).Last(). Therefore one assumes a few Repeating Sections (according to the brief docs) only fields might also work like that. I've also slimmed the calc to =UriNoPath.Split(['.'], (UriNoPath.Split('.')).Count() - 1).Last(). I have a sneaking suspicion using a two step process and the Count() First(), and Last() functions one could get any array item one wishes.
    – Eliot
    Jun 29, 2021 at 22:47
0

So, @MarkShamy is right, however it will only work for a 0 value.

In order to make Cognito Forms actually pick an item from an array, you need to make it calculate something.

I've tried putting any calculations within the usual array pick square brackets, but that doesn't work unless you are changing something on the form.

So the only way that you can pick an item from an array using the usual split()[#] method would be to use this:

=Input.Split('Ð')[Int32(ArrayItem)-1]

Where:

  • Input is the input field which will contain values that have the "Ð" symbol separating them.
  • ArrayItem is a separate input field that you will input whichever item from the array that you want.

However that's imperfect, as I have to physically select that each time.

Solution

Since it won't work how you'd think it should, one needs to use first last logic around the number that you want. In my tests, this works every time ... so if you wish to specify the number explicitly (example "3"):

=Input.Split(['Ð'], 3).Last().Split('Ð').First()

Or if you wish to have the number generated somewhere else:

=Input.Split(['Ð'], Int32(ArrayItem)).Last().Split('Ð').First()

What this does is:

  1. Limit the split to create only the number of items specified. In the above example that is "3".
  2. It then takes the last item, which will always contain unsplit data of at least 2 items.
  3. It then splits that last item again, taking only the first item.

Used in an example: https://www.cognitoforms.com/BleedingCool/POCArrayItemPicker

This logic (perhaps modified) should soundly allow you to select array items in any system (not just cognito forms) where your functionality would potentially be limited by system setup.

So, to apply this to the original question, the answer would be to place the following in the Arraya field if I wanted the 4th item every time:

=Inputta.Split(['#'], 4).Last().Split('#').First()

Or add an ArrayItem field and use the following if I wished to pick an item from the list:

=Inputta.Split(['#'], Int32(ArrayItem)).Last().Split('#').First()

Obviously adding Custom Errors to ensure that any entries in ArrayItem are not larger than the Inputta.Split(['#']).Count().

If you wanted to be flash you could even put a spinner on a ArrayItem and ensure that the minimum number is 1, and the maximum is =Input.Split('Ð').Count(). You could use this array picking to hide data in a field, and have this power much more complicated forms!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.