John Merc
John Merc

Reputation: 53

Using contains after using filter in Julia

I am trying to check if there are other files that doesn't contain the sub string "Sys" but does end with ".vm", but I am getting an error saying :

ERROR: LoadError: MethodError: no method matching contains(::Array{String,1}, 
::SubString{String})
Closest candidates are:
contains(::Function, ::Any, ::Any) at reduce.jl:664
contains(::AbstractString, ::AbstractString) at strings/search.jl:378

This is the code:

    for f2 in filter(x -> endswith(x, ".vm"), readdir())

            if  (!contains(f2,"Sys"))
              #Do something with this file
            end
    end

I encountered the same problem after using readlines on the output of the filter:

    for f3 in filter(x -> !startswith(x, "Sys"), readdir())
                calledFileFunction = readlines(f3)
                fileLength=length(calledFileFunction)
                for i=1:fileLength
                    if  (contains(calledFileFunction,"ball"))
                           #Do something
                    end                  
                end
    end

Is there a way to cast f2 to regular substring and not "Array{String,1}" so I could use contains ?, otherwise how should I implement those functions ?

Upvotes: 0

Views: 1031

Answers (1)

daycaster
daycaster

Reputation: 2697

In your second example, calledFileFunction appears to be an array of strings. So contains needs to be applied to each element of the (to be renamed...) array...

@show typeof(...) is a useful thing to insert into your code if you're getting started.

Something like this might do:

for f3 in filter(x -> startswith(x, "test"), readdir())
   filecontents = readlines(f3)
   fileLength=length(filecontents)
   if any(map(f -> contains(f, "julia"), filecontents))
        println("$f3 contains \"julia\"")
   end                  
end

Upvotes: 2

Related Questions