Problem with length()

Below is a piece of code that I use to analyze values retrieved from MySQL and placed in a data frame.
The if… part is still in its infancy, so please don’t judge it too harshly.

val = df[!,"d8"]
    println("val  ", val)
    val1 = string.(val)
    println("val1  ", val1)
    aa =length(val)
    println(aa," ", val)
    ab = length(val1)
    println(ab,"  ", val1)
    
    if length(val1) == 1
        val1[1] in valid_values ? println("OK val1[1]") : println(val1[1], " THE VALUE IS NOT PRESENT IN 'valid_values' ARRAY ")
    else val1 in valid_values ? println("OK val1") : println(val1, " THE VALUE IS NOT PRESENT IN 'valid_values' ARRAY ")

The values read from the database can be one or two characters long. Depending on this value and the value of the variable, the program evolves differently.
The problem arises when I try to determine the length of the variable. In both cases, the length is 1, which does not seem correct to me.
Below are some printouts with two- and single-character strings.

val  Union{Missing, String}["S"]
val1  ["S"]
1 Union{Missing, String}["S"]
1  ["S"]
OK val1[1]
val  Union{Missing, String}["GM"]
val1  ["GM"]
1 Union{Missing, String}["GM"]
1  ["GM"]
OK val1[1]

val here is a length-1 vector. I suspect you meant to call length(first(val)) or length(only(val))

1 Like
julia> ["S"]
1-element Vector{String}:
 "S"

julia> ["GM"]
1-element Vector{String}:
 "GM"

these are both length-1 because you’re checking the length of the vector.

@ Jling
My biggest problem right now is getting the cell value in string format.

that would be because val = df[!,"d8"] is not a cell value — it is an entire column (which in your case apparently has only one row).

you’ll want to pass a row index, like df[i, "d8"] to receive a single cell value

1 Like

@adiens I get UndefVarError: inot defined inMain`

i here would be an index variable. you need to set it to a value.

1 Like