2

Hi guys i got this ampscript with help from this community.
I want to be able to add a text depending if the output is plural or not.

%%[
set @phone_number_list = "1111111;1111111;1111111;2222222;1111111;3333333"

/* initialize empty collection */
set @phone_number_collection = "" 

set @rows = BuildRowsetFromString(@phone_number_list, ";")

for @i = 1 to RowCount(@rows) do
  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  /* wrapped in arbitrary delimiters to ensure unique string */
  set @phone_number_item = Concat("~", @phone_number, "~")

  /* only output if phone number doesn't exist in collection */
  if IndexOf(@phone_number_collection, @phone_number_item) < 1 then
]%%

%%=V(@phone_number)=%%<br>

%%[
  endif

  /* add phone number to collection */
  set @phone_number_collection = Concat(
    @phone_number_collection,
    @phone_number_item
  )

next @i
]%%

Output
1111111
2222222
3333333

I would like to add some text. If output contain more than one unique phone number, I would like to add: "Your phone numbers" (Plural) and if output only contain one phone number add "Your phone number".

Desired output If output contains more than one phone number:

Output:
1111111
2222222

Your phone numbers 1111111, 2222222

Desired output If output contains one phone number:

Output:
1111111

Your phone number 1111111

4

You could do something like this:

%%[

set @list = "1111111;1111111;1111111;2222222;1111111;3333333"
set @collection = "" 
set @rows = BuildRowsetFromString(@list, ";")
set @unique = 0

for @i = 1 to RowCount(@rows) do

  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  if @i < rowcount(@rows) then 
    set @phone_number_item = Concat(@phone_number, ", ")
  else
    set @phone_number_item = Concat(@phone_number)
  endif

  if IndexOf(@collection, @phone_number_item) < 1 then
     set @collection = Concat(@collection, @phone_number_item)
  endif

next @i

if rowcount(BuildRowsetFromString(@collection, ", ")) > 1 then
  output(concat("Your phone numbers: ", @collection))
else
  output(concat("Your phone number: ", @collection))
endif

]%%

Output:

Your phone numbers: 1111111, 2222222, 3333333

With a single number:

%%[

set @list = "1111111"
set @collection = "" 
set @rows = BuildRowsetFromString(@list, ";")
set @unique = 0

for @i = 1 to RowCount(@rows) do

  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  if @i < rowcount(@rows) then 
    set @phone_number_item = Concat(@phone_number, ", ")
  else
    set @phone_number_item = Concat(@phone_number)
  endif

  if IndexOf(@collection, @phone_number_item) < 1 then
    set @collection = Concat(@collection, @phone_number_item)
  endif

next @i

if rowcount(BuildRowsetFromString(@collection, ", ")) > 1 then
  output(concat("Your phone numbers: ", @collection))
else
  output(concat("Your phone number: ", @collection))
endif

]%%

Output:

Your phone number: 1111111

Updated to handle a single duplicated number:

%%[

set @list = "1111111;1111111"
set @collection = "" 
set @rows = BuildRowsetFromString(@list, ";")
set @rowCount =  RowCount(@rows)

for @i = 1 to @rowCount do

  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  if indexOf(@collection, @phone_number) == 0 then

    if @i == @rowCount then 
      set @collection = concat(@collection, @phone_number)
    else
      set @collection = concat(@collection, @phone_number, ", ")
    endif

  endif

next @i

if substring(@collection,subtract(length(@collection),1),2) == ", " then
   set @collection = substring(@collection,1,subtract(length(@collection),2))
endif

if rowcount(BuildRowsetFromString(@collection, ", ")) > 1 then
  output(concat("<br>Your phone numbers: ", @collection))
else
  output(concat("<br>Your phone number: ", @collection))
endif

]%%

Output:

Your phone number: 1111111

5
  • Fast one Adam ;) – Johannes Schapdick Jul 31 '19 at 12:18
  • set @phone_number_list = "1111111, 1111111" or set @phone_number_list = "1111111, 1111111" big difference, comma or semicolon? With comma it does not work because the seperator is a semicolon ; – Johannes Schapdick Jul 31 '19 at 13:29
  • Hi Adam! Thank you very much for this. However I have found an error that i have tried to fix edititing you ampscript, but I can't make it work. If I set @phone_number_list = "1111111;1111111" The output is: Your phone numbers 1111111 – Martin Ho Jul 31 '19 at 13:32
  • Updated my answer – Adam Spriggs Jul 31 '19 at 14:06
  • Thank you Adam! This is great. It is very appreciated. I must be honest. I am a bit starstruck by you. Have read some many question where you have solved the question. – Martin Ho Jul 31 '19 at 15:10
0

I will edit this code with comments later on ;) But since Adam already answered with a ampscript-block only code, i would take his one. I just edited your code which was not ideal, Adam edited it to the next level. But both of them deliver the expected result (i believe).

%%[
set @phone_number_list = "1111111;1111111;1111111;2222222;1111111;3333333"

/* initialize empty collection */
set @phone_number_collection = "" 

set @rows = BuildRowsetFromString(@phone_number_list, ";")

set @row_count = RowCount(@rows)
if @row_count == 1 then
  set @row = Row(@rows, 1)
  set @phone_number = Field(@row, 1)
]%%

%%=Concat("Your phone number ",@phone_number)=%%<br>

%%[
else
for @i = 1 to @row_count do
  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  /* wrapped in arbitrary delimiters to ensure unique string, i Dont get this therefore deleted the Concat~ */
  set @phone_number_item = @phone_number

  /* only output if phone number doesn't exist in collection */
  if IndexOf(@phone_number_collection, @phone_number_item) < 1 then
    /* add phone number to collection */
    if @i == 1 then 
       set @phone_number_collection = @phone_number_item
    else
    set @phone_number_collection = Concat(@phone_number_collection," , ",@phone_number_item)
    endif
  endif
next @i
]%%
%%=Concat("Your phone numbers ",@phone_number_collection)=%%<br>
%%[
endif
]%%

My second approach for your Duplicate but same number problem would have been (taking adams first code):

%%[

set @phone_number_list = "1111111;1111111;1111111;1111111;1111111"
set @phone_number_collection = "" 
set @rows = BuildRowsetFromString(@phone_number_list, ";")
set @counter = 0

for @i = 1 to RowCount(@rows) do

  set @row = Row(@rows, @i)
  set @phone_number = Field(@row, 1)

  if @i < rowcount(@rows) then 
    set @phone_number_item = Concat(@phone_number, ", ")
  else
    set @phone_number_item = Concat(@phone_number)
  endif

  if IndexOf(@phone_number_collection, @phone_number_item) < 1 then
    set @phone_number_collection = Concat(@phone_number_collection, @phone_number_item)
    set @counter = Add(@counter,1)
  endif

next @i

if rowcount(BuildRowsetFromString(@phone_number_collection, ", ")) > 1 and @counter > 1 then
  output(concat("Your phone numbers: ", @phone_number_collection))
else
  output(concat("Your phone number: ", @phone_number))
endif

]%%
5
  • Hi Johannes - Thank you for the comments. It is very useful for a beginner as me. As i have written to Adam, there is flaw when the set @phone_number_list = "1111111, 1111111" The output is: Your phone numbers 1111111 – Martin Ho Jul 31 '19 at 12:42
  • set @phone_number_list = "1111111, 1111111" or set @phone_number_list = "1111111, 1111111" big difference, comma or semicolon? With comma it does not work because the seperator is a semicolon ; – Johannes Schapdick Jul 31 '19 at 13:28
  • Sorry, it is semicolon. It works fine with semicolon, but the output will only show one number, but the text is the plural text "Your phone numbers" – Martin Ho Jul 31 '19 at 13:31
  • updated my answer with a different method for a duplicate of the same number – Johannes Schapdick Jul 31 '19 at 14:21
  • Thank you Johannes! It worked. Vielen Dank! I will now try to understand the code, so I can do it myself in the future :) – Martin Ho Jul 31 '19 at 15:08

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.