1

I'm trying to split the header row of a pipe delimited text data file and return the index of two fields that could be in any position as the structure of our data varies from campaign to campaign. An example of my code is:

ArrayList list = new ArrayList()
def headingslist = "URN|BATCHID|CUST_URN|CUSTOMER_NAME|POSTCODE|PERSONKEY|MOBILENUMBER||PREMIUM|STATUS"
headingslist.split("\\|")

list.add(headingslist)

int indexMobNo = list.indexOf("MOBILENUMBER")
int indexURN = list.indexOf("PERSONKEY")

However, when I run this code or variations of it, I get the index returned as -1 as it can't find either substring in my string.

1 Answer 1

2

Calling split will not modify the value of headingslist but return a List. So you can either assign the result to list directly or use list.addAll to add all elements of the result to the list. Note that add would add the List itself as a new element to the List. So you would end up with a List that contains one element that is a List.

def headingslist = "URN|BATCHID|CUST_URN|CUSTOMER_NAME|POSTCODE|PERSONKEY|MOBILENUMBER||PREMIUM|STATUS"
ArrayList list = headingslist.split("\\|")

int indexMobNo = list.indexOf("MOBILENUMBER")
int indexURN = list.indexOf("PERSONKEY")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help, that's perfect
You're welcome! Don't forget to mark the question as answered so other know that they don't need to look into it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.