How to return a match of a single string to an array of strings

I would like to return, if one exists, the match between a single string and a string array, or slice. if it doesn’t exist, it just skips or returns blank. Basically acts like a failed with function.

Specifically, in a shortcode, I have a named argument p and I want to compare if its contents match a slice of {{ slice "center" "left" "right" }}

I thought intersect would be what I want but I’m getting error calling intersect: can't iterate over string.

I also tried in and that returns true or false and it works but it also means I need extra lines to flesh out the rest, instead of just working with the match itself.

{{ $p := "a" }}
{{ $s := slice "a" "b" "c" }}

{{ and (in $s $p) $p }} --> returns matching string or false

{{ index (intersect (slice $p) $s) 0 }} --> returns matching string or nil
1 Like

Perfect, thank you for both options. I eventually discovered the last one with some work but I was thinking of something like the first one. Exactly what I needed.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.