A basic foreach issue | Community
Skip to main content
Jo_Pitts1
Community Manager
July 14, 2020
Solved

A basic foreach issue

  • July 14, 2020
  • 1 reply
  • 5511 views

Basic as in simple.. not as in BASIC (sorry - old person language joke)

I have this simple piece of code

 

#set ($seminarList = "A;1|B;5|C;3") #foreach ($myitem in $seminarList.split("[|]")) ${myitem} #end

 

And it is spitting the dummy (completely refusing to do anything useful).

I was expecting it to spit out A;1 B;5 C;3 (obviously, no styling on this yet, but that's not the current issue)

What am I managing to miss here? 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman
#set( $allSeminarDetails = [ {"CODE":"GNG","BLURB":"Come to GNG and learn about Poodles"}, {"CODE":"POY","BLURB":"It's POYsonal Now"}, {"CODE":"GUL","BLURB":"GULivers travels have nothing on life here"}, {"CODE":"WTG","BLURB":"Waiting Waiting Waiting"} ] ) #set( $currentSeminarList = $lead.metlifecompetitiondatas2 ) #set( $currentSeminarCodes = $currentSeminarList.split("[|]") ) ## this list will hold matched seminars #set( $currentSeminarDetails = [] ) ## find matching seminar in master list #foreach( $code in $currentSeminarCodes ) #foreach( $detail in $allSeminarDetails ) #if( $detail.CODE.equals($code) ) #set( $void = $currentSeminarDetails.add($detail) ) #break #end #end #end ## iterate over matches only #foreach( $seminar in $currentSeminarDetails ) ${seminar} #end

1 reply

SanfordWhiteman
New Participant
July 14, 2020

That's the correct syntax to split on the pipe character (which is escaped by being in the character class []).

 

I assume you have this in a Velocity {{my.token}} in an email, yes?

Jo_Pitts1
Jo_Pitts1Author
Community Manager
July 14, 2020

@sanfordwhiteman yes - this is in a token.  

I've tweaked things and made it behave, so now my next challenge.

How to take my split string, and find each matching value in an array.

I've got to this point:

 

#set( $seminarDetails = [ {"CODE":"GNG","BLURB":"Come to GNG and learn about Poodles"}, {"CODE":"POY","BLURB":"It's POYsonal Now"}, {"CODE":"GUL","BLURB":"GULivers travels have nothing on life here"}, {"CODE":"WTG","BLURB":"Waiting Waiting Waiting"} ]) #set ($seminarList = ${lead.metlifecompetitiondatas2}) #set ($seminarArray = $seminarList.split("[|]")) #foreach ($seminarCode in $seminarArray) ## how to find the SeminarCode in the SeminarDetails array. #end

 

 

Let's assume the passed in field {lead.metlifecompetitiondatas2} contains POY|GUL.

My foreach loop will iterate twice (happy day).  What I want to do is get access to the appropriate blurb in the array at the top for output in the email (without doing some silly nested foreach loop on the seminarDetails array).

And I am sure I could condense some of this (i.e. splitting the marketo field straight into an array), but I like to keep it nice and stepwise so I can understand each bit before condensing once I have a handle on what is going on.

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
July 14, 2020
#set( $allSeminarDetails = [ {"CODE":"GNG","BLURB":"Come to GNG and learn about Poodles"}, {"CODE":"POY","BLURB":"It's POYsonal Now"}, {"CODE":"GUL","BLURB":"GULivers travels have nothing on life here"}, {"CODE":"WTG","BLURB":"Waiting Waiting Waiting"} ] ) #set( $currentSeminarList = $lead.metlifecompetitiondatas2 ) #set( $currentSeminarCodes = $currentSeminarList.split("[|]") ) ## this list will hold matched seminars #set( $currentSeminarDetails = [] ) ## find matching seminar in master list #foreach( $code in $currentSeminarCodes ) #foreach( $detail in $allSeminarDetails ) #if( $detail.CODE.equals($code) ) #set( $void = $currentSeminarDetails.add($detail) ) #break #end #end #end ## iterate over matches only #foreach( $seminar in $currentSeminarDetails ) ${seminar} #end