2

I have a dynamic email where the superscripts have to be in order (e.g. 1,2,3 instead of 1, 7, 25 - they come from different pieces of content and may not be in order).

Someone suggested to use BuildRowsetFromString or ClaimRow but I am not sure how to do it.

Can anyone help?

Thanks, VD

  • Any chance you can create a variable, and just add 1 Add(@var,1) every time the superscript is called? – Eduardo Arias Mar 25 '15 at 15:01
2

It's not elegant, but it'll work:

%%[

set @str = "3,2,5,1,4"

]%%
<br>orig: %%=v(@str)=%%
<script runat="server">

  Platform.Load("Core","1.1.1");

  /* get the AMPScript value */
  /* split it into an array */
  /* sort it */
  /* convert it back to a string */
  var str = Variable.GetValue("@str").split(",").sort(sortNumber).toString();

  Variable.SetValue("newStr",str);

  function sortNumber(a,b) {
    return a - b;
  }

</script>
<br>sorted: %%=v(@newStr)=%%

Output:

orig: 3,2,5,1,4 
sorted: 1,2,3,4,5

Once you have your sorted string, then you can use the BuildRowsetFromString() function and iterate through the values.

1

Vic here is an even less elegant solution mainly since there are no real arrays or sorts or custom functions in ampScript. However, this will get you what you want maybe and certainly explores BuildRowsetFromString().

%%[

/* Theoretical basic sort  */
set @str = "3,74,2,5,1"

SET @array = BuildRowSetFromString(@str, ",")
SET @rc = RowCount(@array)
SET @max = 100

IF @rc > 0 THEN 
FOR @i = 1 TO @max DO
 FOR @j = 1 TO @rc DO
   IF Field(Row(@array, @j), 1) == @i THEN 
    SET @newStr = Concat(@newStr, IIF(Empty(@newStr), "", ","), Field(Row(@array, @j), 1))
   ENDIF
 NEXT @j
NEXT @i
ELSE
 SET @newStr = ""
ENDIF

/* slightly more realistic use case partially redundant to above */

set @footers = "3|footer 3,74|footer 74,2|footer 2,5|footer 5,1|footer 1"
SET @footerArr = BuildRowSetFromString(@footers, ",") 
SET @rc2 = RowCount(@footerArr)
SET @max = 100
SET @count = 0

IF @rc > 0 THEN 
FOR @i = 1 TO @max DO
 FOR @j = 1 TO @rc2 DO
   SET @footerValues = BuildRowSetFromString(Field(Row(@footerArr, @j), 1), "|")
   /*
   Using condensed function calls just for space efficiency 
   Grabbing hard coded rows 1 and 2 because we know the data structure and we are splitting rows based on our delimiter |
   When using BRFS() your fields are numerically identified thus the 1s
   */
   SET @footerNum = Field(Row(@footerValues, 1), 1)
   SET @footerTxt = Field(Row(@footerValues, 2), 1)
   IF @footerNum == @i THEN 
    /* Increment out new count and then build the content */
        SET @count = Add(@count, 1)
    SET @newFooters = Concat(@newFooters, IIF(Empty(@newFooters), "", "
"), @Count, ". ", @footerTxt) ENDIF NEXT @j NEXT @i ENDIF ]%% orig: %%=v(@str)=%%
sorted: %%=v(@newStr)=%%
footers:
%%=v(@newFooters)=%%

Will output

orig: 3,74,2,5,1 
sorted: 1,2,3,5,74 
footers:
1. footer 1
2. footer 2
3. footer 3
4. footer 5
5. footer 74

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.