View Single Post
Old 01-11-2023, 09:33 AM   #475
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,471
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Would there be any general use for a first_in_list(val, separator, value [, value]*) type function?

It would check a list for each pattern in order, and then return the found list item as the value.

Template: first_in_list($tags, ',', "Fanfiction.Crossover")
List contents: Fanfiction.Crossover, Fanfiction.Half-Life & Portal, Fanfiction.Mass Effect
Template value: Fanfiction.Crossover

Template: first_in_list(list, ',', "Explicit", "Mature Audiences", "Teen and Up Audiences", "General Audiences", "Not Rated")
List contents: General Audiences, Explicit, Not Rated, Not Rated
Template value: Explicit

Regex pattern matching may be useful but as with the in/inlist, it lead to unintended results:

Template: first_in_list(list, ',', "foo", "^foobar")
List contents: foobar, foo, bar, bar, foo
Template value: foobar
whoops! the regex matched 'foo'

EDIT: Perhaps as a custom function.
This would really be sub-variant of switch, such as switch_list(). As proposed the function eliminates the second piece of the pair, the result, which I think is over-specialized. I think switch_list() is useful and will look at implementing it.

If I add switch_list() then I will probably have it set the built-in variable '$' to the result of the match. That would let you write (untested):
Code:
switch_if(list, ',',
    '^foo$', $,
    strcat('bar', $#status), $,
    '')
chaley is offline   Reply With Quote