Get item number x in list?

Here’s a simple “What am I missing?” question:

If I have a list, the first Operator and last Operator work nicely to retrieve whatever’s in first or last position…

But suppose I have a variable named position_in_list (say), what’s the simplest way to use it to retrieve the item listed in the corresponding numeric position in the list?

In other words, I want to return the fifth list item when the variable is 5, etc. In FileMaker Pro (my familiar background toolkit), there’s a Choose function which does that. Surely we have something similar?

(Related thought: it would be ideal if our documentation did things like lateral links among very similar Operators such as first and last and whatever it is that I’m seeking here.)

-Springer

G’day,

I think you are looking for “nth.”

Although I doubt many folk would want documentation to work like this, here’s an example of how documentation can be organized in a TiddlyWiki instance: BASIC Anywhere Machine Programming Reference

Thanks, Charlie! Of course, once I see it, it makes sense, but I just ran out of ideas on how to track it down.

Well, “nth” doesn’t really grab one by the jugular, so easy to miss.

For whatever reason, I never think of using “first” and always use “nth[1]” for some goofy reason.

These two filter operators are not always equivalent!

While nth[1] (or nth[]) and first[1] (or first[]) return the same results (the first item in the list), these filter operators return different results when the operand is greater than 1.

To illustrate, let’s suppose we have a list, A B C D E F.

As previously noted, [A B C D E F +[nth[1]] and [A B C D E F +[first[]] will both return “A”.

However…

[A B C D E F +[nth[3]] returns just “C”,
but
[A B C D E F +[first[3]] returns “A B C”

To further confuse the issue, there is the zth[] operator, which works the same as the nth[] operator, but is “0 based” instead of “1 based”. Thus, zth[0] is equivalent to nth[1]. Note that both nth[0] and first[0] will return NO RESULT.

Finally, for completeness, I will also mention last[] and butlast[], as well as rest[] (and its synonyms, butfirst[] and bf[]).

Whee, what fun!

1 Like

Just for completeness also consider using the counter variable in lists;

<$set name=total filter="[tag[$:/tags/Macro]count[]]">
<$list filter="[tag[$:/tags/Macro]]" counter=item>
   <<item>>. <$link/><br><$text text={{{ [<item>match<total>then<total>addprefix[Total = ]] }}}/>
</$list>
</$set>

And is there any way of getting several nth’s from a list?
Like doing
[list[C major scale]nth[1,5,8]]
to get a major chord

1 Like

My instinct would be to do this:

{{{ [list[C Major Scale]nth[1]] [list[C Major Scale]nth[5]] [list[C Major Scale]nth[8]] +[join[ ]] }}}

Aside: isn’t a C Major chord CEG, and isn’t 1,5,8 a C5 (aka power) chord?

Regardless, put the generic version of the filter above in a $list widget, and you can get your equivalent chord in different tunings:

...
{{{ [list[D Major Scale]nth[1]] [list[D Major Scale]nth[5]] [list[D Major Scale]nth[8]] +[join[ ]] }}}
...
{{{ [list[G Major Scale]nth[1]] [list[G Major Scale]nth[5]] [list[G Major Scale]nth[8]] +[join[ ]] }}}
...

Actualy I suspect there is half a dozen ways, perhaps more ways to do what you ask.

To choose which of these approaches a little more context or info would be helpful.

Things to consider

  • do you want to reuse this with other numbers?
  • use nth<varname>
  • use the set widgets index param
  • use nested list widgets

Right you are. I’ve messed thing up here. It should be either

[list[C major scale]nth[1,3,5]]

1   3   5
C D E F G A B C

or

[list[C chromatic scale]nth[1,5,8]]

1         5      8
C C♯ D D♯ E F F♯ G G♯ A A♯ B C

My first attempt was the same, but it seems too hard-coded. Would be great to use nth<varname> as TW_Tones points out below.

Back in my Seagull Merlin (aka “M4”) plucking days, I used to spend a lot of time transcribing guitar/ukulele tabs from whatever tuning to my Merlin’s DAdd tuning, or whatever other retuning I might have applied to my Merlin (CGcc, DGdd…).

So the ability to take any chord for one tuning and figure out the chord for some other tuning was a big deal that would have been helped by TiddlyWiki something silly.

Something pretty re widgets wrapped around something like

<!-- "ni", "nii", "niii" each being a note position in whatever major scale -->>
<<This Scale>> notes:<<ni>>, <<nii>>, <<niii>>
{{{ [list<This Scale>nth<ni>] [list<This Scale>nth<nii>] [list<This Scale>nth<niii>] +[join[ ]] }}}

<<That Scale>>  notes: notes:<<ni>>, <<nii>>, <<nii>>
{{{ [list<That Scale>nth<ni>] [list<That Scale>nth<nii>] [list<That Scale>nth<niii>] +[join[ ]] }}}

Something like that. Would have been awesome.

I was critiqued once for using nth[1] instead of first[]. So I figured it must be some kind of unwritten best practice to use first[] instead of nth[1]. I didn’t agree (i.e. kept on using nth[1]), but I could not be bothered making the case for continuing with nth[1].

Glad I didn’t bother, because I wouldn’t have come anywhere near as good as that great explanation of nth vs first.