Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 11-30-2012, 12:11 AM   #1
Jade Aislin
Groupie
Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.
 
Posts: 164
Karma: 3100
Join Date: Sep 2011
Device: Kobo Auro H2O, PRS-T1
need help with nested template

I've been scouring the reference for functions and the template page in the calibre manual, but there is no example that shows how each term works so I am having trouble understanding it.

I wanted to have a column that would tell me the average words per chapter of each book, so I created a custom column that is built from other columns. I already had a custom column for words and one for chapters.

First I tried this code and received this exception:

Code:
program:divide('#words', '#chapters')

EXCEPTION:  could not convert string to float: #words
Even though both custom columns #words and #chapters are integers, it seems that it is being read as a string.

Here is my next attempt:
Code:
program:
    format_number('#words','{0:d}');
    format_number('#chapters','{0:d}')
While that seemed to be fine, it did not produce any numbers in the column. When I added the divide function, I received the same exception as the first code.

I then tried to combine the divide and format number function together:

Code:
program:divide(format_number('#words','{0:d}'),format_number('#chapters','{0:d}'))

EXCEPTION:  float division by zero
I think that the last one is probably what I want to use. It seems that it is having a problem when it divides by zero, but I am unsure how to fix this.

If I use the second and first code, I would need to assign the result from the format number functions to a variable in order to use the divide function, but I am unsure how to do so.

Any suggestions on how I should change my code?
Jade Aislin is offline   Reply With Quote
Old 11-30-2012, 01:39 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 11,693
Karma: 6240117
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Your problem comes from confusing constants with field (column) values. The string '#chapters' is a constant. It always has the value '#chapters', which as you can see is not a number. To get the value of a metadata field you use the 'field' function. This function takes the name of the field as its first argument. This argument is normally a constant, but certainly does not need to be.

Using the above, your first program would become:
Code:
program:divide(field('#words'), field('#chapters'))
This program can produce floating point output. For that reason you might want to use
Code:
program: format_number(divide(field('#words'), field('#chapters')), '{0:5.0f}')
Finally, if there is any chance that the #chapters field is empty (zero) then you need to test for that before doing the divide. That is more complicated, requiring something like the following:
Code:
program: 
# Get the value of the chapters field so we can use it more easily
chapters = raw_field('#chapters');

# Check if chapters is zero. If it is then put  the empty string into the variable notIsZero
notIsZero = cmp(chapters, 0, 'yes', '', 'yes');

# Ensure that the chapters variable contains a non-zero number. We need this because the divide will happen
# even though we will throw the value away
chapters = test(notIsZero, chapters, '1');

# Return the result of the divide if notIsZero contains the non-empty string, otherwise return the empty string
test(notIsZero,  format_number(divide(field('#words'), chapters), '{0:5.0f}'), '')
chaley is offline   Reply With Quote
Advert
Old 11-30-2012, 02:04 AM   #3
Jade Aislin
Groupie
Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.Jade Aislin could sell banana peel slippers to a Deveel.
 
Posts: 164
Karma: 3100
Join Date: Sep 2011
Device: Kobo Auro H2O, PRS-T1
Quote:
Originally Posted by chaley View Post
Your problem comes from confusing constants with field (column) values. The string '#chapters' is a constant. It always has the value '#chapters', which as you can see is not a number. To get the value of a metadata field you use the 'field' function. This function takes the name of the field as its first argument. This argument is normally a constant, but certainly does not need to be.
Thanks, I didn't know I had to use the 'field' function. Although the description in the reference section of the manual doesn't sound like how you described it (getting the value from the metadata field).

Quote:
Finally, if there is any chance that the #chapters field is empty (zero) then you need to test for that before doing the divide.
Although, I try to make sure that empty chapters fields get a 1, the last code you gave me works perfectly.

Thanks.
Jade Aislin is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested headings? crich70 Sigil 20 04-11-2011 10:44 AM
Content Nested collections fartang Amazon Kindle 1 04-09-2011 12:18 PM
Content nested collections alexxx Amazon Kindle 6 06-22-2010 06:05 PM
Nested navPoint problem Lemming Calibre 3 12-20-2009 11:24 AM
Are Nested TOCs Possible? VikingDave Sony Reader 11 12-28-2007 07:58 PM


All times are GMT -4. The time now is 07:38 AM.


MobileRead.com is a privately owned, operated and funded community.