1

How can I find the last value in an array using AMPscript. I know I can call positions like 1, 2, 3 etc and it will return the values corresponding to those positions but how can I call the last value? Thanks

1 Answer 1

3

Since there are no arrays in AMPScript (just rowsets), you can just use the rowcount() to find the last element:

%%[
var @arr, @e, @arrPos, @count, @lastVal

set @arr = "red|yellow|black|white|green"
set @e = BuildRowsetFromString(@arr,"|")
set @count = rowcount(@e)

if @count > 0 then

    set @arrPos = row(@e, @count) /* get row last element */
    set @lastVal = field(@arrPos,1)
    outputline(concat("<br>lastVal: ",@lastVal))

else

  outputline(concat("<br>empty array"))

endif
]%%
4
  • this works Adam, thanks a lot. Some of these values are repeating, how can I find how many of the @lastVal are?
    – Snowalker
    Oct 26, 2016 at 14:14
  • You can iterate through the elements with a for-loop. I have some examples on my blog here that can be altered to use row-sets from strings: sprignaturemoves.com/ampscript-lookup-examples Oct 26, 2016 at 14:16
  • that works, thanks again. Now the problem is, I need to show the email content at the time of sent but in the view online they change because the ampscript runs again. Any idea how to fix that?
    – Snowalker
    Oct 26, 2016 at 22:02
  • Check out this _messagecontext personalization string Oct 26, 2016 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .