Use search-replace operator to remove commas from a value sourced from another tiddler

I have this code in a plugin that finds the value of a list XPs and uses a filter to sum the values:

<h2>Total XP:
<$calc>
<$set name="count" filter="[enlist:raw{!!creatures}count[]]">
<$list filter="[range<count>]" variable="i">
<$list filter="[enlist:raw{!!creatures}nth<i>]">
{{!!xp}} +
</$list>
</$list>
</$set>
0
</$calc>
</h2>

This is now causing an error with the updated source tiddlers because the new XP fields are now using commas in the numbers. E.g. 1500 is now 1,500. The filter is giving an unable to parse error. I want to add a search-replace operator to the reference to !!xp to remove commas before performing the filter but am having trouble with the syntax required. I’m new to TiddlyWiki and have already spent a few hours reading and playing with syntax but can’t figure it out.Hoping someone can help me.

Obviously this is the incorrect syntax, but I’m hoping to do something like this:

<h2>Total XP:
<$calc>
<$set name="count" filter="[enlist:raw{!!creatures}count[]]">
<$list filter="[range<count>]" variable="i">
<$list filter="[enlist:raw{!!creatures}nth<i>]">
[{{!!xp}}]search-repace[,][] +
</$list>
</$list>
</$set>
0
</$calc>
</h2>

I suspect this might be easier without the $calc widget! My instinct would be to do all the calculations in a single filter, e.g.

<h2>Total XP:
<$text text={{{ [enlist:raw{!!creatures}] +[get[xp]search-replace[,],[]] +[sum[]] }}} />
</h2>

I omitted the count[] step as I’m not sure what it’s adding.

Let me know how it goes!

1 Like

Plugged the above code in and it works perfectly now!

Thanks @etardiff !! I really appreciate the quick response with the fix.

I’ve got a lot of learning to do to fully understand how TiddlyWiki javascript usage works, but at least that solves my immediate issue :smiley:

1 Like

Another approach to search and replace is split and join.

eg

{{{ [[2,00,2334.34]split[,]join[]] }}}
  • Splits the string into characters between the , then joins them back, thus eliminating the commas.
  • Search and replace is more meaningful / semantic, but I use split and join a lot so its straight forward to me.