Velocity Script - Adding 2 times "and" before and after last items of array, and how to remove blank space between array's items | Community
Skip to main content
Cristina_Leonet
New Participant
June 24, 2021
Solved

Velocity Script - Adding 2 times "and" before and after last items of array, and how to remove blank space between array's items

  • June 24, 2021
  • 2 replies
  • 2805 views

Hello!

I'm seeking your help to fix this code that I created to output a list of "true" values split by "," where the last item has an "and" before instead of a comma.

When I run a test, I see the "and" is repeated after and before the last item, and also I'm not able to find out how to remove the blank space between the array products name.

 

Here you can find what I've done

 

 

#set ($Partners = $lead.becomeapartner_test) #set ($BI = $lead.sNBusinessIntelligence) #set ($Cloud = $lead.sNCloud) #set ($Embedded = $lead.sNEmbeddedIntelligence) #set ($Hyper = $lead.sNHyperIntelligence) #set ($Consulting = $lead.sNProfessionalServices) #set ($PartnersValue = "") #if ($Partners=="1") #set ($PartnersValue = "Become a Partner") #end #set ($BIValue = "") #if ($BI=="1") #set ($BIValue = "Business Intelligence") #end #set ($CloudValue = "") #if ($Cloud=="1") #set ($CloudValue = "Cloud") #end #set ($EmbeddedValue = "") #if($Embedded=="1") #set ($EmbeddedValue = "Embedded Analytics") #end #set ($HyperValue = "") #if($Hyper=="1") #set ($HyperValue = "HyperIntelligence") #end #set ($ConsultingValue = "") #if($Consulting=="1") #set ($ConsultingValue = "Consulting") #end #set ($productsValues = [$PartnersValue, $BIValue, $CloudValue, $EmbeddedValue, $HyperValue, $ConsultingValue]) #set ($productsValuesLenght = $productsValues.size()) #set ($Last = $productsValuesLenght - 1) #set ($Start = $productsValuesLenght - 2) #foreach( $productValue in $productsValues ) #if ($productValue != "") $productValue #if($foreach.count <= $Start) , #elseif($foreach.count == $Last) and #end #end #end

 

 

Thanks in advance for your suggestions!

Cheers! 😊 

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

I would do this whole thing more efficiently and extensibly.

 

Always try for a configuration-centric or collection-centric approach. Think of your friendly names as part of your configuration, not your code. Code shouldn't be peppered w/new strings everywhere.

 

Here you see the config — your interesting fields and their friendly labels — at the top. Just loop over them and copy the true ones to a new list. Display that new list.

#set( $fieldsAndFriendlyNames = { "becomeapartner_test" : "Label1", "sNBusinessIntelligence" : "Label2", "sNCloud" : "Label3", "sNEmbeddedIntelligence" : "Label4", "sNHyperIntelligence" : "Label5", "sNProfessionalServices" : "Label6" } ) #set( $trueFields = [] ) #foreach( $fieldDesc in $fieldsAndFriendlyNames.entrySet() ) #set( $fieldRealName = $fieldDesc.getKey() ) #set( $fieldFriendlyName = $fieldDesc.getValue() ) #if( $lead[$fieldRealName].equals("1") ) #set( $void = $trueFields.add($fieldFriendlyName) ) #end #end ${display.list($trueFields)}

 

2 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
June 24, 2021

I would do this whole thing more efficiently and extensibly.

 

Always try for a configuration-centric or collection-centric approach. Think of your friendly names as part of your configuration, not your code. Code shouldn't be peppered w/new strings everywhere.

 

Here you see the config — your interesting fields and their friendly labels — at the top. Just loop over them and copy the true ones to a new list. Display that new list.

#set( $fieldsAndFriendlyNames = { "becomeapartner_test" : "Label1", "sNBusinessIntelligence" : "Label2", "sNCloud" : "Label3", "sNEmbeddedIntelligence" : "Label4", "sNHyperIntelligence" : "Label5", "sNProfessionalServices" : "Label6" } ) #set( $trueFields = [] ) #foreach( $fieldDesc in $fieldsAndFriendlyNames.entrySet() ) #set( $fieldRealName = $fieldDesc.getKey() ) #set( $fieldFriendlyName = $fieldDesc.getValue() ) #if( $lead[$fieldRealName].equals("1") ) #set( $void = $trueFields.add($fieldFriendlyName) ) #end #end ${display.list($trueFields)}

 

Cristina_Leonet
New Participant
June 24, 2021

Flawless as always! Thank you so much! @sanfordwhiteman 

 

I'm really interested in learn more about velocity, do you recommend some learning sources for beginners! 

 

Cheers!

 

 

 

SanfordWhiteman
New Participant
June 29, 2021

I'm really interested in learn more about velocity, do you recommend some learning sources for beginners! 

Well... aside from the resources on my blog (most of which are cross-posted here to the Products Blog) there’s not much. And even those posts aren’t so beginner-ish. You might look up my talk from this year’s Adobe Experience Makers, which gives some new angles. Also plan to host some Velocity webinars soon, if I can remember how to do makeup. 🙂

Jon_Wright
New Participant
June 24, 2021

Going to try and beat @sanfordwhiteman to this one, but this should do it rather than the for loop.

$display.list($productsValues, ", ", " and ")

 Sure Sanford has feedback on how do do all of this more succinctly though.