06-24-2021, 05:46 PM | #1 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Template: Extracting a number from a string
I have two columns, text #kobobookmark and integer #chaptercount.
I'd like to create a new column to keep track of read chapters in a fanfic, e.g. 5/25. I thought the best way to do that is a GPM template that a) checks a book is a fanfic, is currently being read, and has something in #kobobookmark to count, b) extract the number of read chapters from #kobobookmark, c) display that number next to #chaptercount. I managed to get the first part working already: Code:
program: if $#fanficcat && $#currentlyreading && $#kobobookmark then $#chaptercount fi OEBPS/file0003.xhtml#kobo.1.1 The "0003.xhtml" means that it's currently on chapter 3. What would be the best way to get the number and display it as '3/{#chaptercount} chapters read'? Last edited by ownedbycats; 06-24-2021 at 07:11 PM. |
06-24-2021, 09:13 PM | #2 |
Grand Sorcerer
Posts: 24,907
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
You can get that number with regex. The following should return it.
Code:
program: re_group($#kobobookmark, '.*\/file(\d+).*') |
06-24-2021, 09:29 PM | #3 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That covers part b). While I tinker to see if I can figure out c), is there a way to strip excess leading 0s?
|
06-24-2021, 09:42 PM | #4 |
Grand Sorcerer
Posts: 24,907
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
The function "format_number" should do it:
Code:
program: format_number(re_group($#kobobookmark, '.*\/file(\d+).*'),'{0:,d}') |
06-24-2021, 10:14 PM | #5 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I think I'm supposed to use a template() here. Though I syntaxed something wrong because it's dividing the numbers (e.g. in the screenshot, it should be '21/22'). Oops.
Code:
program: if $#fanficcat && $#currentlyreading && $#kobobookmark then template(format_number(re_group($#kobobookmark, '.*\/file(\d+).*'),'{0:,d}')/$$#chaptercount) fi Last edited by ownedbycats; 06-25-2021 at 12:34 AM. |
06-25-2021, 12:47 AM | #6 |
Grand Sorcerer
Posts: 24,907
Karma: 47303748
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
I am fairly sure you don't want "template" there. What you probably want to do is a "strcat". So:
Code:
strcat(format_number(re_group($#kobobookmark, '.*\/file(\d+).*'),'{0:,d}'),'/',$$#chaptercount) Code:
current_chapter = 0; if contains($#kobo_reading_location, 'File','1','0') then current_chapter = re_group($#kobobookmark, '.*\/file(\d+).*') fi; strcat(format_number(current_chapter,'{0:,d}'),'/',$$#chaptercount); |
06-25-2021, 01:05 AM | #7 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That worked, though testing it showed a slight oversight since I tend to pause at the beginning of chapters. I fixed by subtracting by one.
Code:
program: input = $#kobobookmark; a = re(input, '.*\/file(\d+).*', '\1'); if $#fanficcat && $#currentlyreading && $#kobobookmark then strcat(format_number(subtract(a, 1), '{0:,d}'), '/',$$#chaptercount) fi Last edited by ownedbycats; 06-25-2021 at 01:09 AM. |
06-25-2021, 01:21 AM | #8 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I was able to fix it by doing this (for some reason it recognized neither input nor a)
Code:
strcat(format_number(subtract((re($#kobobookmark, '.*\/file(\d+).*', '\1')), 1), '{0:,d}'), '/',$$#chaptercount) |
06-25-2021, 03:07 PM | #9 |
Custom User Title
Posts: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
The error I got turned out to be a bug in the column evaluator. chaley is working on it now.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Template: Incrementing a padded number by one | ownedbycats | Library Management | 2 | 03-09-2021 07:54 PM |
Replace reoccurring string with increasing number | 1v4n0 | ePub | 4 | 12-17-2017 05:35 AM |
Djvu: Extracting ISBN numbers from a large number of books? | MelBr | Other formats | 7 | 04-13-2014 03:35 AM |
Error message: "TEMPLATE ERROR Single '}' encountered in format string" | frank14612 | Calibre | 22 | 07-02-2013 11:05 AM |
Mathch a string while ignoring some character in that string? | ElMiko | Sigil | 12 | 12-01-2011 10:05 PM |