0
votes

What I am trying to do is

i = occursin("ENTITIES\n", lines)
i != 0 || error("ENTITIES section not found")

The error information is

ERROR: LoadError: LoadError: MethodError: no method matching occursin(::String, ::Array{String,1})
Closest candidates are:
  occursin(::Union{AbstractChar, AbstractString}, ::AbstractString) at strings/search.jl:452

This is a piece of julia v0.6 code. I am using v1.1 now. I am new to julia and don't know what's the proper subsititute function for this. Please help.

1

1 Answers

1
votes

You can broadcast orrursin like this (add a . after function name):

julia> x = "abc"
"abc"

julia> y = ["abc", "xyz"]
2-element Array{String,1}:
 "abc"
 "xyz"

julia> b = occursin.(x, y)
2-element BitArray{1}:
  true
 false

julia> findall(b)
1-element Array{Int64,1}:
 1

julia> findfirst(b)
1

Note that although String can be iterated over it is treated by broadcast as a scalar.

Also it is worth to remember that occursin returns Bool value so that you can use it directly in logical tests e.g. i || error("ENTITIES section not found") in the code from your question.

In order to locate the index in the collection of the occurrence of true in the return value of broadcasted occursin use findall or findfirst functions (there is also findlast). The difference is that findall returns a vector of entries where true is encountered in the collection, while findfirst returns the first such entry only. Also note the difference when you pass all falses to it. findall will return an empty vector and findfirst will return nothing.

If you do not want to retain the vector b in the code above, you can get the indices directly (this should be faster) by passing a predicate as a first argument to findall/findfirst:

julia> findall(t -> occursin(x, t), y)
1-element Array{Int64,1}:
 1

julia> findfirst(t -> occursin(x, t), y)
1