user9990604
user9990604

Reputation:

How to store vector of string in Julia?

I have two numerical one dimensional vector (A,B) of size ~800000, and through some hashing operation these two are combined together and produce a string of size 6. Now I don't know how to store these strings? Whatever I do, it gives me an error.
I've tried using ArrayArray{string}(undef, 6) and also Dict.

My try is something like this:

# import pakages: read from csv, hashing
using DelimitedFiles
using GeohashHilbert

csvfilename = "C:/Users/lin/Desktop/uber-raw-data-jul14.csv"
csvdata     = readdlm(csvfilename, ',', header=true)
data        = csvdata[1]
header      = csvdata[2]

lat  = data[:,2]
long = data[:,3]

lat_len     = length(lat)

#GeoHashed  = GeohashHilbert.encode(lon, lat, precision, bits_per_char)
GeoHashed = Dict()


for i in 1:lat_len
    GeoHashed[i]   = GeohashHilbert.encode(long[i], lat[i], 6, 6)
end


What's the issue??


ERROR: LoadError: MethodError: no method matching isless(::Int64, ::SubString{String})
Closest candidates are:
  isless(::AbstractString, ::AbstractString) at strings/basic.jl:344
  isless(::Any, ::Missing) at missing.jl:88
  isless(::Missing, ::Any) at missing.jl:87
  ...

Upvotes: 1

Views: 441

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

It looks like you're feeding strings into the encode function which doesn't work:

julia> GeohashHilbert.encode("51.1", "0.5", 6, 6)
ERROR: MethodError: no method matching isless(::Int64, ::String)
...
Stacktrace:
 [1] <(x::Int64, y::String)
   @ Base .\operators.jl:352
 [2] <=(x::Int64, y::String)
   @ Base .\operators.jl:401
 [3] encode(lon::String, lat::String, precision::Int64, bits_per_char::Int64)
   @ GeohashHilbert C:\Users\ngudat\.julia\packages\GeohashHilbert\vh6xu\src\GeohashHilbert.jl:118
 [4] top-level scope
   @ REPL[62]:1
 [5] top-level scope
   @ C:\Users\ngudat\.julia\packages\CUDA\KnJGx\src\initialization.jl:52

You probably meant to do:

julia> GeohashHilbert.encode(51.1, 0.5, 6, 6)
"W13T@3"

so your problem is likely reading in the data. It's impossible to tell without having the csv file available, but I'm assuming if you did typeof(lat) you would get Vector{SubString{String}} instead of Vector{Float64} as you seem to expect.

So the solution is probably to use a more fully featured CSV reader like CSV.jl to read your csv file to ensure that you end up with numerical data, or do parse.(Float64, lat) to convert your data after reading it in from csv.

Upvotes: 2

Related Questions