Concatenating strings is not obvious

Is there a reason why concatenating strings is not as simple as in other programming languages?

For example,I just now had to resort to the following

$basetitle={{{ [{$:/TLS/incremental}addsuffix[.]addsuffix{$:/TLS/storage_format}addprefix{$:/TLS/filenameprefix}] }}}

to just create a tiddler with a title like YB1234.jpg

I know there are other ways to do this, most probably more sophisticated ways, but it seems to me to be very non obvious when other languages just use a concatenation operator like + or &

Just curious

bobj

You can use a function to simplify it a bit

\function basetitle() [{$:/TLS/filenameprefix}] [{$:/TLS/incremental}] "." [{$:/TLS/storage_format}] +[join[]]

and use it like $basetitle=<<basetitle>>

1 Like

I find it easier to visualize concatenation by avoiding addprefix/addsuffix.

Refactoring your code so I can easily tweak+test:

<$let basetitle={{{ [[$:/TLS/incremental]addsuffix[.]addsuffix[$:/TLS/storage_format]addprefix[$:/TLS/filenameprefix]] }}}.>
<<basetitle>>
</$let>

<$let basetitle={{{ [[$:/TLS/filenameprefix]] [[$:/TLS/incremental]] [[.]] [[$:/TLS/storage_format]] +[join[]] }}}.>
<<basetitle>>
</$let>

The ways in which concatenation is done in other programming languages, I would not want them in TiddlyWiki.

1 Like

@Charlie_Veniot

Your example code should be using curly braces ([{...}]) to fetch values from their respective tiddlers. As written, they are using doubled square brackets, which treats the tiddler titles as literal text.

-e

1 Like

For testing purposes, it is a huge pain in the rear to create fields for the specific filter.

Easier to just replace those curly brackets with square brackets just to demonstrate the different ways to write the filter.

If I had been provided with tiddlers for import in to a TiddlyWiki with which to test, I would have stuck with the curly brackets.

I do not like to provide filter expressions I cannot test, and I don’t have the time/energy to create the things to test filter expressions that require specific things.

The point was to provide two different filters which anybody can try without needing anything other than a TiddlyWiki instance.

1 Like

:100:    

This in my view illustrates a common problem when coming to tiddlywiki with knowledge of other programming languages.

It is actually easier to concatenate strings in tiddlywiki than any other language I have come across.

I won’t give an exhaustive proof here but I assert the truth in the above self quote and ask you to keep an open mind and you will start to see this is true.

Of course when, where and what, you wish to concatenate makes it a little more complex and this includes the concatenation of strings, variables, field and tiddler contents.

Your example is concatenating the contents of two tiddlers, and a string . to provide the result as a parameter.

  • The key here is you want to provide a parameter crafted from the concatenation of other values.

In this case I recommend; for a result you wish to use many times use one of following:

  • Set widget using the filter parameter on the set widget
  • Define a function as in @Mohammad case, the advantage is you can introduce a meaning full name for the result using it many times and it’s definition is clearly stated at the top of your tiddler or as a global function.
    • Within a filter you can use the add prefix and suffix operators or the new substitute operator if there is a lot of static text containing values.
    • Functions need “join” to return all but the first value but can be used out of the box to obtain the first value which may be a common requirement for some.

If however you wish to concatenate some values once off and inline without making it reusable, I would still consider the above approach for consistency. Otherwise;

  • Use the triple curly braces as did @Charlie_Veniot which uses filters
  • Use the new backtick parameters form to construct your parameter from variable references or filters.

I have not included the following as there are complications with when and if they are evaluated. Which is nessasary if it’s to be used as a parameter

  • `/define macroname()’ still usable despite being depricated
  • `/procedure procame()’

Both of these above are fine if you simply want to display the result, just not if you want to use them as parameters.

  • I believe Exceptions exist, but this is the safest approach to avoid wasting time.

I can give examples of your use case for each of my recommended approaches if asked.

After thought

It is important to note there is no value in tiddlywiki that can’t be referenced using filter notation, so learning each of these makes it simple to write functions, filters using triple curly braces and backtick parameters;

  • With the advantage in backtick parameters of simple string and variable concatenation

@Mohammad I gave your post a :open_mouth: because I would never have dreamed the parser would handle string literals in the middle of a filter.

But thinking on it, if it can treat "this as a list" as a filter expression, then of course it should work! I’m truly shocked I never thought to even try it. So, thank you, Mo!

Anyway, just for the hell of it…

\function cat(a, b, c) "a is " [<a>] " and b is " [<b>] " and c is " [<c>] "." +[join[]]

<<cat "a letter" "another letter" "yet another letter">>

Output: a is a letter and b is another letter and c is yet another letter.

No prizes for guessing where that is going to come in really handy…

See: https://tiddlywiki.com/#Filter%20Run

For the +[join[]] trick (which is TW5’s nearest cousin to “concatenate” whatever the filter yield is, so far):

Shouldn’t we make sure to document the need for = prefix, to guard against unexpected results when duplicates appear in the filter?

{{{ 
[[string1]] =[[string2]] ="." =[[string3]] +[join[]] 
}}}

gets you string1string2.string3

If we don’t model it this way, then when we swap things in to make a variant where what’s in the string1 position happens to yield the same value as something later in the filter, the first occurence is dropped (because of Dominant Append), right?

Everyone on this thread may be familiar with this, but for the sake of others visiting this thread…

Thank you everyone who has contributed to this discussion. I understand from @TW_Tones comments that there are going to be differences depending on what the concatenated string is to be used for. As a non-expert TW coder, I like @Mohammad 's solution the best, it seems simple and easily understandable without complexifying (if the word exists) already confusing lines of script.

bobj