Register Guidelines E-Books Today's Posts Search

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

Notices

Reply
 
Thread Tools Search this Thread
Old 01-07-2023, 12:32 PM   #1
jwahome2002
Junior Member
jwahome2002 began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
Stuck on If, then, else

Hello - very new to Calibre, but very familiar with data manipulation. I am trying to create a custom field using if-then-else. I have spent days searching and cannot figure out the correct syntax.

Here is what I am trying to solve for:

IF {universe} isempty

THEN concatenate({universe},{book#})

ELSE concatenate ("1",{author},{saga},{Book#}


Any help is greatly appreciated!!
jwahome2002 is offline   Reply With Quote
Old 01-07-2023, 04:29 PM   #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: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by jwahome2002 View Post
Hello - very new to Calibre, but very familiar with data manipulation. I am trying to create a custom field using if-then-else. I have spent days searching and cannot figure out the correct syntax.

Here is what I am trying to solve for:

IF {universe} isempty

THEN concatenate({universe},{book#})

ELSE concatenate ("1",{author},{saga},{Book#}


Any help is greatly appreciated!!
You didn't give the real column lookup names so I am guessing. And what you wrote doesn't make a lot of sense. For example, why are you concatenating an empty field (universe) with book#? And what is book#?

Assuming you meant not isempty on the IF, that "universe" is a custom column with the lookup key #universe, that "saga" is a custom column with the lookup name #saga, and "book#" is the calibre book id, I think you want something like this in General Program Mode:
Code:
program:
	if $#universe then
		strcat($#universe, $id)
	else
		strcat('1', $authors, $#saga, $id)
	fi
chaley is offline   Reply With Quote
Advert
Old 01-08-2023, 09:14 PM   #3
jwahome2002
Junior Member
jwahome2002 began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
Thank you Chaley! This has gotten me so much closer to my goal! I would never have figured out closing with 'fi'.

Here are the field names (assuming you are asking for the Lookup name vs Column Header): #universe, #saga, #book_num, authors, title

You are correct - the true statement is when universe is NOT blank. Mis-type on my end.

My library is primarily fantasy novels. Some of the sets (e.g. Forgotten Realms) are sorted by their book order, the rest I want sorted by author and saga or author and title.

Does Calibre allow one to pull out the author last name? Or at least use a string that has last name first? This field will be ultimately used as a sort.

Here are examples of what I am trying to do:

Universe sample:
Universe = Forgotten Realms
Book# = 26
Desired Result = Forgotten Realms26

Book w Saga
Universe = (blank)
Author = Terry Brooks
Saga = Saga of Shannara
Book# = 12
Title = Bearer of the Black Staff
Desired Result = Brooks, TerrySaga of Shannara12

Book w no Saga
Universe = (blank)
Author = Terry Brooks
Saga = (blank)
Book# = (blank)
Title = Daughter of Darkness
Desired Result = Brooks, TerryDaughter of Darkness

Does the syntax here follow Excel rules, where once you get past the first criteria, you no longer have to reference it? In other words, can I just perform an ELSEIF where Saga is NOT BLANK or do I still need to reference the Universe criteria?

Thank you for any assistance you can offer!
jwahome2002 is offline   Reply With Quote
Old 01-09-2023, 08:31 AM   #4
jwahome2002
Junior Member
jwahome2002 began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
Hey Chaley!

I figured out the last name sort by using the following:

program:
if $#universe then
strcat($#universe, $#book_num)
else
strcat('1', $author_sort, $#book_num)
fi

I have tried various combinations for 'else if', but keep getting errors. I'm really close. If you can help me with this last bit of syntax, it would be greatly appreciated!
jwahome2002 is offline   Reply With Quote
Old 01-09-2023, 08:47 AM   #5
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: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
This does what your examples show, with the exception that I added commas so I could read the output. Note that your examples didn't show the '1' so I removed it.

The lines at the top are there so I could test it. I don't have your columns so I simulated them using variables. You can remove these lines and use the column lookup names directly.

The field author_sort contains the value calibre uses to sort. If there is more than one author, this template chooses the first author in the author_sort list.

The template language syntax is closer to visual basic than to excel. The General Program Mode documentation might help.

This should be enough to get you going.

Code:
program:
#	universe = $#universe;
#	universe = 'Forgotten Realms';
	universe = '';

#	book_num = $#book_num
	book_num = 26;

#	sage = $#saga
#	saga = 'Saga of Shannara';
	saga = '';

#	title = $title
	title = 'Bearer of the Black Staff';

#	author_sort = $#author_sort
	author_sort = 'Brooks, Terry';

	if universe then
		strcat(universe, ', ', book_num)
	elif saga then
		strcat(list_item(author_sort, 0, '&'), ', ', saga, ', ', $id)
	else
		strcat(list_item(author_sort, 0, '&'), ', ', title)
	fi
chaley is offline   Reply With Quote
Advert
Old 01-09-2023, 03:00 PM   #6
jwahome2002
Junior Member
jwahome2002 began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
Chaley - THANK YOU!!!

I had to make some minor mods to get it to work, but it works!

program:
if $#universe then
strcat($#universe, $#book_num)
elif $#saga then
strcat(list_item($author_sort, 0, '&'), $#saga, $#book_num)
else
strcat(list_item($author_sort, 0, '&'), $title)
fi

What programming language is this? I know VBA and SQL. I'd like to get more familiar with this.

One last question if I may - the book number is not sorting correctly. I have a list that the system sorts 1, 10, 11, 2... I cannot figure out what the format is to add preceding zeros.

Much appreciated!!
jwahome2002 is offline   Reply With Quote
Old 01-09-2023, 04:03 PM   #7
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: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by jwahome2002 View Post
What programming language is this? I know VBA and SQL. I'd like to get more familiar with this.
It is a language I made up for fun, basing it on ideas from functional programming and several languages of my youth: Algol 68, lisp, and Pascal. Nobody liked the functional stuff, which is why I added Pascal/Algol statements a while back. You can see the language grammar and examples in the documenation I linked to.

Note also that you can write templates in pure Python 3, should that be of interest. The documentation tells you how to do it. You would need to look at the calibre database api.
Quote:
One last question if I may - the book number is not sorting correctly. I have a list that the system sorts 1, 10, 11, 2... I cannot figure out what the format is to add preceding zeros.

Much appreciated!!
Use the format_number() function. Here is an example formatting an integer as 3 digits with leading zeros. It makes a list of them, sorts the list, then displays it. Try it in the Template Tester dialog.
Code:
program:
	output = '';
	for i in range(40, 0, -5):
		output = output & format_number(i, '03d') & ','
	rof;
	list_sort(output, 0, ',')
chaley is offline   Reply With Quote
Old 01-09-2023, 06:08 PM   #8
jwahome2002
Junior Member
jwahome2002 began at the beginning.
 
Posts: 5
Karma: 10
Join Date: Jan 2023
Device: Calibre
THANK YOU!
jwahome2002 is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuck on 5.4.3.3 VelvetElvis Calibre Companion 2 05-10-2020 01:38 AM
Stuck BeccaPrice Writers' Corner 3 05-27-2013 11:03 AM
i am stuck me too Workshop 14 07-25-2012 02:38 AM
K3 got stuck wyndslash Amazon Kindle 18 10-16-2011 08:16 PM


All times are GMT -4. The time now is 10:47 PM.


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