Appending to a string inside a repeat

Hi,

I always face this issue and wanted to get your perspective on the right way to do this,
I want to have one string that concatenates several rows of a repeat region and put a new line between each row. At the end of the repeat, I want to be able to return the resulting string.

This is how I approached it:

However, the variable research seems empty after each iteration and output is returning a space only.

Here are the formulas used at each step:

Before the repeat:
research = ' '

Inside the repeat:
research = research+'Page Title: \'\'\''+title+'\'\'\', Snippet: \'\'\''+snippet+'\'\'\', Link: \'\'\''+link+'\'\'\' \n\n'

Outside the repeat:
output = research

It seems like the research variable inside the repeat is reinitialised at each iteration
and that the research variable after the repeat is taking the value of the research variable that was before the repeat.

Any ideas of how to solve this?
Many thanks

Hello,

Your Set Values must make use of the global name, e.g.: research_g

Don’t use the same names for global and non-global, otherwise you’ll run into collision issues

Also, instead of referencing research in your Set Value expressions, reference research_g

1 Like

Ah wow! Thank you @Apple that’s great, based on your answer I have also looked into: Set value - Name and Global Name values discussion

And it now works:
Before the repeat:

Inside the repeat:

Outside the repeat:

1 Like

Happy to help!

In case you find the concatenation speed a bit slow, I recommend writing a custom formatter - but that’s just if you find it slow.

In my case, I had to concatenate many lines (+1000), and found concatenation through Wappler steps is significantly slower than concatenation directly in NodeJS (or PHP) :slight_smile:

1 Like

Why not use the array component for that? You can use it exactly as a string builder:

3 Likes

Interesting, will do that, thanks

@Apple @Teodor is this global variable definition in wappler shared across the entire application instance? Meaning is it equivalent to

global.sharedVariable = “This is a shared variable”;

in other terms, when i have multiple users and they’re both trying to access or change this variable, they’re going to see each other’s changes because there’s only one instance of that global variable?

or will it have different values for each user?

They’re scoped to each request, it’s not truly global

1 Like

That’s great then. Thanks