# Problem with length()

**URL:** <https://discourse.julialang.org/t/problem-with-length/129938>\
**Category:** New to Julia\
**Created:** [June 16, 2025, 5:13pm UTC](https://discourse.julialang.org/t/problem-with-length/129938 "2025-06-16T17:13:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FSUfan](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@FSUfan](https://discourse.julialang.org/u/FSUfan)\
**Post date:** [June 16, 2025, 5:13pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/1 "2025-06-16T17:13:52Z")

</div>

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.

```julia
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.

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

```

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

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [June 16, 2025, 5:17pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/2 "2025-06-16T17:17:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 16, 2025, 6:01pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/3 "2025-06-16T18:01:19Z")

</div>

```julia
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.

---

<div class="post-metadata">

**Author:** ![FSUfan](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@FSUfan](https://discourse.julialang.org/u/FSUfan)\
**Post date:** [June 16, 2025, 6:14pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/4 "2025-06-16T18:14:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [June 16, 2025, 6:17pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/5 "2025-06-16T18:17:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FSUfan](https://avatars.discourse-cdn.com/v4/letter/f/838e76/32.png) [@FSUfan](https://discourse.julialang.org/u/FSUfan)\
**Post date:** [June 16, 2025, 6:26pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/6 "2025-06-16T18:26:04Z")

</div>

@adiens I get `UndefVarError: `i`not defined in`Main`

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [June 16, 2025, 6:31pm UTC](https://discourse.julialang.org/t/problem-with-length/129938/7 "2025-06-16T18:31:59Z")

</div>

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