Custom collumn returns "TEMPLATE ERROR list index out of range"

Bug #1946342 reported by LilYoda
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Fix Released
Charles Haley

Bug Description

Hello. Since last update I did (5.27, but still occurs in 5.28), one of my custom collums randomly returns "TEMPLATE ERROR list index out of range"

Screenshot below

The formula for this collum is {#authors_namesinverted:'re($, ' & ', ', ')'}
The idea is that when there are multiple authors, they are separated by a , and not a &
This allows my kobo to detect multiple authors for a book, instead of thinking that it's a single author named "author1 & author2"

Regarding #authors_namesinverted, I'm not sure if standard or not in Calibre. The idea is to have all author names display as "FirstName LastName", whereas they are recorded as "LastName, FirstName" in the "author" collumn
The formula for it is {authors:'list_re_group($, ' & ', '.', "(^.*\, .*$)|(^.*$)", "[[$:'re($, '^([^,]+), ([^[]+)', '\2 \1')']]", "[[$]]")'}

To be noted: there is no error anywhere in the calculation for this formula. Only in the calculation of the first formula in this bug report

What is more weird: when I click on the field displaying "TEMPLATE ERROR list index out of range", it opens the "edit template" window, showing the formula. And in this window, the result is correct, and shows the correct author name.

See screenshot there:

If I click OK, then the complete database is recalculated, but for some reason, the bug reappears (and not always on the same books)

Revision history for this message
LilYoda (lilyoda) wrote :
Revision history for this message
Charles Haley (cbhaley) wrote :

I need an exception trace. Run calibre in debug mode (click the down arrow on Preferences and choose Restart in debug mode) and post the exception(s) from the log.

FWIW: your #authors_namesinverted column can be better expressed as
  {authors:'list_re_group($, ' & ', '.', '(^.*$)', '[[$:swap_around_comma()]]')'}
or as I would prefer:
  program: list_re_group($authors, ' & ', '.', '(^.*$)', '{$:swap_around_comma()}')

And unless you have a reason to have two columns I would do this to build the inverted comma-separated list directly from the authors column:
 res = '';
 for author in $authors separator ' & ':
  author = swap_around_comma(author);
  if !res then
   res = author
   res = strcat(res, ', ', author)

I included the above in the attached txt file in case launchpad breaks the lines in strange ways

Changed in calibre:
assignee: nobody → Charles Haley (cbhaley)
Kovid Goyal (kovid)
Changed in calibre:
status: New → Incomplete
Revision history for this message
LilYoda (lilyoda) wrote :

Attached is debug log

I clicked twice on a cell showing the "TEMPLATE ERROR list index out of range", once on a book by bowden, once on a book by Troisi

Revision history for this message
Charles Haley (cbhaley) wrote :

You should be able to work around the problem by using the template
    program: re($#authors_namesinverted, ' & ', ', ')
instead of
    {#authors_namesinverted:'re($, ' & ', ', ')'}

Changed in calibre:
status: Incomplete → Fix Committed
Revision history for this message
Kovid Goyal (kovid) wrote : Fixed in master

Fixed in branch master. The fix will be in the next release. calibre is usually released every alternate Friday.

 status fixreleased

Changed in calibre:
status: Fix Committed → Fix Released
Revision history for this message
LilYoda (lilyoda) wrote :

Thanks to you both. I decided to go as you mentioned earlier and a single column, and building the comma-separated inverted list directly from the author column. It works fine and turns out I didn't really need two columns

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.