It looks you it would be so much easier if you just run
readcvs("test.csv").
On Tuesday, October 13, 2015 at 5:41:27 AM UTC+2, masa charlie wrote:
>
> Hi, Jacob
>
> Thanks for the reply, and the way you showed to use "enumerate()" is
> really nice to know of, and it has eventually given me a great insight to
> deal with the case.
> What I was trying to do was to read data from csv file, and getting a list
> of array (array of array) out of it.
> Here are what I wanted to do, and how I have got it done in the following
> way; let me share what I'd like to do more specifically here:
>
> This is a csv file named "test.csv"
>
> aaa, john, 100
> bbb, mike, 200
> ccc, steve,300
> ddd, mary, 400
>
> Then here goes the code, "samp.jl"
>
> f = open("./test.csv")
>
> res = Array{Any}[] ## I found this is the key!!!
>
> for line in eachline(f)
> val = split(strip(line), ",")
> push!(res, val)
> end
>
> print("--------BEFORE---------\n")
> print(res)
>
> ## code from Jacob here !
> for elm in res
> for (i, v) in enumerate(elm)
> parsed = tryparse(Int, v)
> elm[i] = isnull(parsed) ? v : get(parsed)
> end
> end
>
> print("\n")
> print("--------AFTER---------\n")
> print(res)
>
> close(f)
>
> Then, results are as follows.
>
> julia> include("samp.il")
> --------BEFORE---------
> Array{Any,N}[Any["aaa"," john"," 100"],Any["bbb"," mike","
> 200"],Any["ccc"," steve","300"],Any["ddd"
> ," mary"," 400"],Any[""]]
> --------AFTER---------
> Array{Any,N}[Any["aaa"," john",100],Any["bbb"," mike",200],Any["ccc","
> steve",300],Any["ddd"," mary"
> ,400],Any[""]]
> julia>
>
>
> It turns out that you have to declare an array variable to store the
> results with Array{Any}[] in advance,
> otherwise, (meaning in such a case as you declare "res = []" without type
> being clarified), you'd eventually have an array with Array{String}[], and
> you cannot convert ASCIIString represented numeric values into Int due to
> type mismatch.
>
> Anyway, thanks for your help!!
>
> Best regards
>
> Masa
>
>
>
>
> On Tuesday, October 13, 2015 at 2:46:18 AM UTC+9, Jacob Quinn wrote:
>>
>> I'd probably do something like:
>>
>> julia> x = Any["abc", "10"]
>> 2-element Array{Any,1}:
>> "abc"
>> "10"
>>
>> julia> for (i,v) in enumerate(x)
>> parsed = tryparse(Int,v)
>> x[i] = isnull(parsed) ? v : get(parsed)
>> end
>>
>> julia> x
>> 2-element Array{Any,1}:
>> "abc"
>> 10
>>
>>
>> On Mon, Oct 12, 2015 at 8:31 AM, masa charlie <[email protected]>
>> wrote:
>>
>>> I have a question:
>>> I want to know the way to convert a ASCIIString represented number in an
>>> Array{ASCIIString, 1} into Int, and put it back into the original list as
>>> Array{Any, 1} in Julia.
>>>
>>> For example:
>>>
>>> When I have a array variable x as follows;
>>>
>>> julia> x = ["abc", "10"]
>>> 2-element Array{ASCIIString,1}:
>>> "abc"
>>> "10"
>>>
>>> and I want to get it converted as the following.
>>>
>>> julia> y = ["abc", 10 ]
>>> 2-element Array{Any,1}:
>>> "abc"
>>> 10
>>>
>>> Then, if I've tried applying parse function and putting it back to the
>>> original Array, I ended up getting an error below.
>>>
>>> x[2] = parse(Int, x[2])
>>> ERROR: MethodError: `parse` has no method matching parse(::Type{Int64},
>>> ::Int64)
>>> Closest candidates are:
>>> parse{T<:Integer}(::Type{T<:Integer}, !Matched::Char)
>>> parse{T<:Integer}(::Type{T<:Integer}, !Matched::Char,
>>> !Matched::Integer)
>>> parse{T<:Integer}(::Type{T<:Integer}, !Matched::AbstractString,
>>> !Matched::Integer)
>>> ...
>>> in convert at none:1
>>> in setindex! at array.jl:313
>>>
>>> Can any one have a suggestion on the way to get an expected result?
>>>
>>>
>>> Thanks in advance!!!
>>>
>>> Masa
>>>
>>>
>>>
>>>
>>