Hello Everybody,
Let’s say I have a 2-element array containing one floating and a string, something like
2-element Array{Any,1}:
"0 0"
1
I want to check whether the array contains the string “0 0”. I tried isassigned()
, but it does not work with strings. Do you have any suggestions?
Thank you for your time
Best
The following should work:
"0 0" in array
3 Likes
oheil
June 12, 2021, 3:47pm
3
Or, in the case that you don’t know the exact string and want to know if there just is a String:
julia> String in typeof.(array)
true
2 Likes
Or any(isa.(array, String))
(which potentially does fewer type checks)
2 Likes
Actually, benchmarking it, my method has a slightly worse best case and a much worse worst case. But for a majority of cases it’s faster. So I guess I don’t know.
1 Like
oheil
June 12, 2021, 5:42pm
6
Perhaps this is the fastest:
any( x -> isa(x,String), array )
4 Likes
It is indeed. By two orders of magnitude. I keep forgetting about that version of the any
family of functions.
(also, looking forward to this, if it ever happens:
JuliaLang:master
← tkf:curry-isa
opened 01:13AM - 22 Jul 20 UTC
This PR adds `===(x)`, `!==(x)`, and `isa(T)` that creates a `Fix2` like `isequa… l` etc. For `===(x)` and `isa(T)`, it looks like I needed to add tfuncs.
fix #36163
fix #32018
)
1 Like