[Request] Simple array like data in Tiddlywiki

There are cases, I need arrays in Tiddlywiki!

A simple example: In a list I want to display each entry with a dedicated color, so when I iterate on a list items I need to read colors from an array like color(1), color(n)

I know I can add a field to each tiddler and set colors or use a data dictionary with 1:red, 2:yellow, …
But I am looking for a dynamic way as I can use in scripts!

What do you suggest?

Ref: JavaScript Arrays

2 Likes

would something like this work?

<$list filter="[{!!items}split[ ]]" counter="index" variable="item">
	<$list filter="[{!!colors}split[ ]nth<index>]" variable="color">
		<<item>> is <<color>>
		<br>
	</$list>
</$list>

field colors = #ffffff #ff0000 #00ff00 #0000ff #ffff00
field items = one two three four five

Like @admicos I use a similar approach, but I also miss a simple array like data in Tiddlywiki.

\define gci() [<data>split[;]trim[]prefix[$(i)$]split[:]trim[]last[]]

<$vars data="1:red; 2:yellow; 3:brown">
<$list filter="[range[1,3]]" counter=i>

color(<<i>>) is:  <$text text={{{ [subfilter<gci>] }}} />
</$list>
</$vars>

Produces


color(1) is: red

color(2) is: yellow

color(3) is: brown

You may also like to have

<$vars i=3> {{{ [subfilter<gci>] }}} </$vars>

<$vars i=2> {{{ [subfilter<gci>] }}} </$vars>

which produces

brown
yellow

But I am sure having simpler, more readable code (where core support arrays) is preferable.

It’s an interesting question. I had to do something similar recently:

\define colours() red green blue orange

<$let numberOfColours={{{ [<colours>enlist-input[]count[]] }}}>
	<$list filter="[all[tiddlers]sort[]]" counter="counter">
		<$let colourIndex={{{ [<counter>subtract[1]remainder<numberOfColours>add[1]] }}}>
			<div style={{{ [<colours>enlist-input[]nth<colourIndex>addprefix[color:]addsuffix[;]] }}}>
				<$text text=<<currentTiddler>>/>
			</div>
		</$let>
	</$list>
</$let>

Output:

<div style="color:red;">
"A free, open source wiki revisited" by Mark Gibbs, NetworkWorld
</div>
<div style="color:green;">
"A Thesis Notebook" by Alberto Molina
</div>
<div style="color:blue;">
"ATWiki" by Lamusia Project
</div>
<div style="color:orange;">
"BJTools" by buggyj
</div>
<div style="color:red;">
"BrainTest - tools for a digital brain" by Danielo Rodriguez
</div>
...
3 Likes

enlist<xx> should do the same thing:

<$let numberOfColours={{{ [enlist<colours>count[]] }}}>

Not really an improvement, but you could use the zth operator to eliminate the add filter operator.

...
	<$let colourIndex={{{ [<counter>subtract[1]remainder<numberOfColours>] }}}>
			<div style={{{ [<colours>enlist-input[]zth<colourIndex>addprefix[color:]addsuffix[;]] }}}>
...

If $let could dynamically reassign a variable, then this arguably simpler version could be used:

<$let numberOfColours={{{ [<colours>enlist-input[]count[]] }}} curColor="orange" >
	<$list filter="[all[tiddlers]sort[]]" counter="counter">
		<$let curColor={{{ [<curColor>cycle<colours>] }}}>
			<div style={{{ [<curColor>addprefix[color:]addsuffix[;]] }}}>
				<$text text=<<currentTiddler>>/>
			</div>
		</$let>
	</$list>
</$let>
1 Like

Thank you all!

So in summary:

  1. a variable (a macro) can hold the colors like a 1D array
  2. using enlist and nth operator I can retrieve element(i) of this array!
  3. I need to use filter to get elements of these data arrays

I would just add that in my view, many things can be considered an array in tiddlywiki a list of titles can be accessed via an index with the set widget, and filters can be used to manipulate, select, count entries in almost any list, so in some ways arrays are everywhere but nowhere. The list field used by tags or even each row in a text field are similar. And now with the new widgets that use the $fields and $values parameters such as

Hackability Improvements