I am trying to get a dynamic variable set inside a script block with AMPScript.
Here is what I got:
FOR @i = 1 to 5 DO TreatAsContent( Concat( "SET @Test", @i, " = ", 'some value' ) ) NEXT @id SET @somethingElse = @Test1
@Test1 does not exists.
How do I get a dynamic variable name created inside a loop?
Here is my working solution in AMP Script (no SSJS)
%%[ SET @someValue = 'Test' SET @anotherSample1 = 'Sample1' SET @anotherSample2 = 'Sample2' SET @anotherSample3 = 'Sample3' SET @anotherSample4 = 'Sample4' SET @anotherSample5 = 'Sample5' FOR @i = 1 to 5 DO /* Create a variable Test1, Test2 ... with the value of variable @someValue */ TreatAsContent( Concat( "%", "%[ SET @Test", @i, " = '", @someValue, "']%", "%" ) ) /* Access a variable based on the loop integer */ SET @var = TreatAsContent( Concat("%%=v(@anotherSample",@i,")=%%") ) /* Create a variable AnotherSample1, AnotherSample2 ... with the value of variable @anotherSample1, anotherSample2 ... */ TreatAsContent( Concat( "%", "%[ SET @AnotherSample", @i, " = '", @var, "']%", "%" ) ) NEXT @id /* OUTPUT: @Test1 = 'Test' @Test2 = 'Test' @Test3 = 'Test' @Test4 = 'Test' @Test5 = 'Test' @AnotherSample1 = 'Sample1' @AnotherSample2 = 'Sample2' @AnotherSample3 = 'Sample3' @AnotherSample4 = 'Sample4' @AnotherSample5 = 'Sample5' ]%%