View Single Post
Old 07-16-2022, 12:52 PM   #3
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,452
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ackomb View Post
So I've been fiddling with it some more, and figured out I can use the same principle as this

Code:
program:
	a = list_intersection($ao3tags, 'tram, train, bus, ferry', ',');
	list_union($#genre, a, ',')
but instead use 're' in the place of list-union

Code:
program:
        a = list_intersection($#show, 'Ferry, Bus', ',');
        re($#show, a , 'Vehicles, Public Transport')
The issue lies within the following, if a story has the following
tags: Bus, Car, Ferry, Ferry Terminal,
the result will be:
tags: Vehicles, Public Transport, Vehicles, Public Transport Terminal, Red, Car


The tag 'Ferry Terminal' will result in 'Public Transport Terminal'.
I figured the reason is because it is a match but not an exact match.
I've tried several options trying to get an exact match using ^ and $ like with regex but I keep getting errors

Can anyone point me in the right direction, because the Calibre Manual is not getting me anywhere on this one.
I am lost. What are you trying to do? Is this the problem you talked about 2 posts back where you want to replace any/all of 'tram, train, bus, ferry' by one instance of 'public transport'? If so, what is the list_intersection for? It returns a list that contains bus and/or ferry, if these two items exist in $#show. The order is not guaranteed.

Lets say you are trying to replace 'Ferry' or 'Starship' with 'Vehicle', and 'Bus' with 'Public Transport'. This does it.
Code:
program:
	sample_input = 'Bus, Car, Starship, Ferry, Ferry Terminal, Alien Spacecraft';

	output = list_re_group(
		sample_input, ',', '.',
		'(^bus$)|(^ferry$|^starship$)', 'Public Transport', 'Vehicle')
The output of the template is
Code:
Public Transport, Car, Vehicle, Ferry Terminal, Alien Spacecraft
There are other ways to do it, including processing the list with a 'for' statement then checking the items.

One thing to know: re() is nearly useless when dealing with lists. It doesn't deal with list order, instead treating the list as text. In certain situations such as my example above you can use list_re_group() so that the regular expression is applied to each list item, not the list itself.

If this discussion will go on for a while then it should get moved to its own thread. I can do that.
chaley is offline   Reply With Quote