# Last row is broken when iterating an array

**URL:** <https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696>\
**Category:** General Usage\
**Tags:** iterative, array, csv\
**Created:** [September 30, 2018, 12:33pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696 "2018-09-30T12:33:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![loleg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loleg/32/2200_2.png) [@loleg](https://discourse.julialang.org/u/loleg)\
**Post date:** [September 30, 2018, 12:33pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696/1 "2018-09-30T12:33:15Z")

</div>

When trying to load a CSV file and use a custom iterator over its columns, the last row is broken in Julia 1.0. I’ve had to update the iterator approach, but the basic code was working in 0.6. Yes, I did test this with no warnings in 0.7.

This behavior looks like a bug to me and possibly related to [#28763](https://github.com/JuliaLang/julia/issues/28763). Also if you have a better idea for this use case using built-in libraries, please feel free to share.

```julia
using DelimitedFiles # for readdlm

mutable struct mytbl # a custom object
    source
    function mytbl(csvdata::Base.GenericIOBuffer)
        source = readdlm(csvdata, ',')
        source = convert(Array, source[2:end,:]) # clear the headers
        new(source)
    end
end

Base.length(it::mytbl) = size(it.source, 1)

function Base.iterate(it::mytbl, (el, i)=(it.source[1,:], 1))
   return i >= length(it) ? nothing : (el, (it.source[i + 1,:], i + 1))
end

# some sample data
TABLE_CAST = """id,height,age,name,occupation
1,10.0,1,string1,2012-06-15 00:00:00
2,10.1,2,string2,2013-06-15 01:00:00
3,10.2,3,string3,2014-06-15 02:00:00
4,10.3,4,string4,2015-06-15 03:00:00
5,10.4,5,string5,2016-06-15 04:00:00
"""

table = mytbl(IOBuffer(TABLE_CAST))

[row for row in table]
# the last element is #undef

[row[1] for row in table ]
# Returns:
[1, 2, 3, 4, 229445824]

[row[2] for row in table ]
# Returns:
[10.0, 10.1, 10.2, 10.3, 4.94e-324]

```

Note that I’ve tested this with more or less elements, the last row is always…well, broken.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 30, 2018, 12:36pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696/2 "2018-09-30T12:36:09Z")

</div>

Can you reproduce the problem by looking directly at the result of `readdlm`, without the `mytbl` wrapper? In any case, I’d file an issue on GitHub.

You can try with CSV.jl or CSVFile.jl, which are more powerful anyway, especially when columns have different types.

---

<div class="post-metadata">

**Author:** ![loleg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loleg/32/2200_2.png) [@loleg](https://discourse.julialang.org/u/loleg)\
**Post date:** [September 30, 2018, 2:48pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696/3 "2018-09-30T14:48:09Z")

</div>

Thanks @nalimilan, advice taken.

`readdlm` loads the table just fine. It’s during the iteration that passing the element around seems to break things. Please advise if I should file an issue.

Meanwhile I’ve managed to work around the problem with this iterator:

```julia
Base.length(it::mytbl) = size(it.source, 1)
function Base.iterate(it::mytbl, (el, i)=(nothing, 1))
    if i > length(it); return nothing; end
    return (it.source[i,:], (nothing, i + 1))
end

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 30, 2018, 4:00pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696/4 "2018-09-30T16:00:43Z")

</div>

That sounds weird. I’d definitely file an issue.

---

<div class="post-metadata">

**Author:** ![loleg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/loleg/32/2200_2.png) [@loleg](https://discourse.julialang.org/u/loleg)\
**Post date:** [September 30, 2018, 4:33pm UTC](https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696/5 "2018-09-30T16:33:00Z")

</div>

Done, thanks:

[https://github.com/JuliaLang/julia/issues/29443](https://github.com/JuliaLang/julia/issues/29443)
