Merging two variables into a new one?

Hi,

I have two variables, one is the name of a field, the other, the name of a tiddler
I am trying to create a new variables that will be “{{tiddler!!field}}”.
I know how to create a variable by adding new strings to a pre-existing one.
But I can’t find how to merge two variables into one. the second variable added simply overwride the first.

is it possible to set a variable from multiple pre-existing variables ?

My last attempt, were P2=P1. I try to merge the variable “field” and "rules( into "{{rules!!field}} I also tried with “addprefix” or “addsuffix”, but the variable inside of the “add” is ignored.

<$set name="P1" value={{{ [<field>addsuffix[}}]addprefix[!!]] }}}> => this work
<$set name="P2" value={{{ [<Rules><P1>] }}}> => here P2= the last variable added
test : <<P1>> <br>
test : <<P2>> <br>
</$set>
</$set>
\define title() abc
\define field() text

\function f.getText() [<title>get<field>]

<<f.getText>>

{{{ [<title>get<field>] }}}

Ok, I though going straight to the point would be better, but apparently, context is important when you ask a question… :sweat_smile:
Sorry pmario, my question wasn’t precise enough.

Please, allow me to explain what I am trying to do.

I currently have multiple “rules” tiddlers containing similar but not always identical fields with different values. Some of theses fields, with their name prefixed with a “Line_” contain HTML codes for table lines with number or texts in their columns.

An other field of the rules tiddlers, common to every single of them, also contain what I call my “import manager”.
It is a list of checkboxes in list mode that will write fields names from their rules-tiddler inside a specific field of the tiddler where they are transcluded. These names are the names of the fields that contain the html lines.
example:

this is in a field of the "Rules_" tiddler and is transcluded inside "topic tiddlers"
<$tiddler tiddler=<<HERE>>>
 <$checkbox listField="Import" checked="{{Rules_AAPG!!Line_Innov}}"> AAP Innov</$checkbox><br />
 <$checkbox ... [insert as many as needed, but manually, in each rules tiddler]
</$tiddler>

I then have “topic tiddlers”.
Among other thing, they transclude this “import manager”, thus displaying a list of checboxes that will add their “checked” value into a list-field.

this is in the "Topic" tiddler.
field:"Import"
content: "{{Rules_AAP1!!Line_Innov}} {{Rules_AAPG!!Line_Deca}}" (content varies based on checked boxes)

<table>
{{!!Import}}
</table>

This will transclude all the transclusion indicated in “import” as lines in the table.

<table>
{{!!import}}
</table>
became :
<table>
 {{Rules_AAPG!!Line_Innov}}{{Rules_AAPG!!Line_Deca}}
</table>
and thus :
<table>
 <tr>
  <td><!--Icon-->  </td>
  <td><!--Name--> </td>
  <td><!--Value--> </td>
 </tr>
 <tr>
  <td><!--Icon-->  </td>
  <td><!--Name--> </td>
  <td><!--Value--> </td>
 </tr>
</table>
displaying "Lines_" selected by the user

It work perfectly, but I have to manually create the list of checkboxes.
I am trying to automatize this procedure.
The objective remain to write transclusion names inside the “import” field of the current tiddler.

<$let HERE=<<currentTiddler>> Rules={{!!RulesName}}>
 <$tiddler tiddler={{!!RulesName}}>
  <$list filter="[all[current]fields[]prefix[Line_]]" variable="field">
   <$list filter="[all[current]get<field>]" variable="value">
    <$set name="RulesP1" value={{{ [<field>addsuffix[}}]addprefix[{{!!]] }}}>
     <$set name="RulesP2" value={{{ [<Rules>addsuffix[<RulesP1]addprefix[{{]] }}}>
      <$checkbox tiddler=<<HERE>> listField="Import" checked=<<RulesP2>>> <<field>></$checkbox><br />
     </$set>
    </$set>
   </$list>
  </$list>
 </$tiddler>
</$let>

The objective is to generate, for each “Line_something” field in the tiddler specified in {{!!RulesName}}, a checkbox that will add the value {{NameOfTheRuleTIddler!!Line_something}} into the list that is stored in the field {{!!Import}}.

I got everything to work, with the exeption of assembling the transclusion code, as the two variable won’t merge into a new one.
I can already create checkboxes that will add {{!!Line_something}} to the list-field. I want to add the tiddler name into this.

Well, of course, if someone have a better idea to manage these imports, I am open to any suggestion…
But the objective remain to give the opportunity to a final user to select fields to import as line into a table, just with checkboxes.

To sum it up, I have two variables, <<a>> and <<b>>, and I want to transform them into a variable <<ab>>, but I don’t know how to do it, if only it is possible.

maybe your example should have


    <$set name="RulesP1" value={{{ [<field>addsuffix[}}]addprefix[!!]] }}}>
     <$set name="RulesP2" value={{{ [<Rules>addsuffix<RulesP1>addprefix[{{]] }}}>

These two lines could be written using a single $let widget, like this:

<$let RulesP2={{{ [[{{]] [<Rules>] [[!!]] [<field>] [[}}]] +[join[]] }}}>

-e

2 Likes

I guess I would use

<$let RulesP2=`{{$(Rule)$!!$(Field)$}}`>

less risk of chevron blindness!

1 Like

The word “merge” may be inappropriate here, join, concatinate, construct, substitute, are more suitable, because merge tends to imply an “interleaving”.

When ever a text reference can be used tiddlername!!fieldname check to see if the tiddler="tiddlrename" field="fieldname" parameters are available because you can skip this step of concatenating them.

If you must concatinate them to get a text reference;

With recent tiddlywiki versions we have a number of ways to “concatenate” two or more strings. See functions and backtick attributes.

Functions

\function test.reference() [<tiddlername>] [[!!]] [<fieldname>] + [join[]]

<<test.teference>> or param=<<test.teference>>
  • Unlike macro and procedures the functions will have their filters evaluated before using the attribute/value pair.
  • Functions used as variables <<functionname>> will only return the first value, use join to make it into a single value if needed.

Backtick attributes;

Substituted Attribute Values

param=`$(tiddlername)$!!$(fieldname)$`

Substitute Operator

The new substitute operator could also be used

\function text.reference() [[$1$!!$2$]substitute<tiddlername>,<fieldname>]

<<text. Reference>>

Short transude {{tiddler!!field}}

Keep in mind this is the short form of the transclude, by using the $transclude widget you gain access to the $tiddler and $field attributes. If you cant use the long form widget you can simply replace the {{tiddler!!field}} with one of the forms that retrieve the value in tiddler/field as per the others who replied above.

Final note;

  • The need to make a text reference from variables may be suggested when reading some widgets syntax, however the tiddler/fieldname parameters may be available, but the code can usually be rewritten in a way that this is not required.

Thank you.
The +[join[]] was the part I missed.
I think I had read about it at some point, but never used it, and completely forget its purpose. I’ll make sure to remember it this time.

About the set vs let, I originally used let, but I had decomposed my code to see where it was failing, before tweaking each part again and again. Generally without restarting from a clean version after each failed attempt.
Probably a bad habit that I should get ride off if I want to progress…

1 Like

Oops, please disregard. I have to read this discussion a few times over to try and figure out the use case.

Ah, I got it. Very interesting stuff.