title: AMPSCRIPT - String building with a for loop
body: <p>I want to print out the different names of friends that are in this database.  Depending on the name chosen, it'll print out the list of friends associated with that name.  </p>
  
  <pre><code>%%[
  
      FOR @y = 1 TO @friendRowcount DO 
          Set @friendRow = Row(@friendRows,@y)
          Set @friendName = Lookup("DB_FriendIDToName","friend_name","friendid",FIELD(@FriendRow,"friendid"),"language",Lowercase(@lookupLang))    
  
          Set @NameList = CONCAT(@NameList, @friendName, ", ")
  
      NEXT @y
      Set @bodytext = Replace(@bodytext,"PLACEHOLDERFRIENDNAMES", @NameList)
  
  
      Set @bodyText = "Your friends are PLACEHOLDERFRIENDNAMES."
  ]%%
  
  %%=v(@bodyText)=%%
  </code></pre>
  
  <p>The problem is that it only prints out one name and not the comma separated list. Seeing how the list will usually be a different length then the others, I don't know how to do this dynamically.  </p>
  
  <p>The output I want to see is "<code>Your friends are Name1, Name2, Name3, and Name4.</code>"</p>
  
  <p>I am currently seeing "<code>Your friends are Name3</code>"  because it is replacing <code>@Name</code> (and in turn <code>@NameList</code>) each time it looks instead of adding it.</p>
  
  <p>edit: updated the code and it is a step in the right direction.  The last issue I ran into was figuring out how to tackle the " and Name#"  portion.</p>