Am 06.09.19 um 11:08 schrieb Riccardo (Jack) Lucchetti:
On Thu, 5 Sep 2019, Artur Tarassow wrote:
> Hi all,
>
> quite frequently I need to scan a string array for a specific string
> value. Either I need just a boolean return for (non-)existent but
> sometimes also the entry number of string s2 in array s2.
>
> I've written two function proposals (see attached files or here:
>
https://github.com/atecon/instrings) which may be added to the
> extra.gfn package if you agree. What I've called instrings_pos()
> [name it just instrings()] would be the more general idea though.
I like these, and I think this is potentially useful. There's only one
thing I'd like to think about a bit more. In some cases, one may need
the equivalent of the ".=" operator, that is a function that does not
just return the first occurrence, if any, but a whole binary array.
Modifying your function instrings_pos() to this effect would be
trivial. Do you think it worthwhile?
Good idea about which I was also thinking. An I think it's nice to have
the more general form. I re-named the function to instrings() and
dropped the former instrings() which simply returned a boolean. The now
instrings() function returns a column matrix with the positional indices
if s2 occurs in array s1.
<hansl>
function matrix instrings (const strings s1, const string s2)
/* Returns the positional entries of s2 in s1 in a column vector
if s1 contains s2, 0 otherwise. */
if nelem(s1) == 0
printf "Warning: String array '%s' is empty.\n", argname(s1)
return {0}
endif
matrix ret = {}
loop i=1..nelem(s1) -q
if s1[i] == s2
ret |= $i
endif
endloop
if rows(ret)>0
return ret
else
return zeros(1,1)
endif
end function
</hansl>
Best,
Artur