# How do I get readdlm to return a Matrix of strings?

**URL:** <https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186>\
**Category:** New to Julia\
**Tags:** strings, generator, io, lazy-evaluation\
**Created:** [August 2, 2022, 7:04pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186 "2022-08-02T19:04:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyeHawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eyehawk/32/36399_2.png) [@eyeHawk](https://discourse.julialang.org/u/eyeHawk)\
**Post date:** [August 2, 2022, 7:04pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/1 "2022-08-02T19:04:14Z")

</div>

I am reading in a list of english words into my program. I then want to make sure the words are lowercase so I am processing the list with lowercase() one word at a time and pushing them into another list. It seems that I run into problems when I hit a keyword for Julia like the word “false” in the original list.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 7:11pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/2 "2022-08-02T19:11:11Z")

</div>

Could you show use a minimum working example as well as the error that you are encountering? Calling `lowercase("false")` seems to work just fine.

```julia
julia> lowercase("false")
"false"

```

Expanding to an array of strings seems to work fine as well:

```julia
julia> keywords = ["Struct", "MUTABLE", "False", "TRUE", "1", "Symbol"]
6-element Vector{String}:
 "Struct"
 "MUTABLE"
 "False"
 "TRUE"
 "1"
 "Symbol"

julia> lowercase.(keywords)
6-element Vector{String}:
 "struct"
 "mutable"
 "false"
 "true"
 "1"
 "symbol"

```

---

<div class="post-metadata">

**Author:** ![eyeHawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eyehawk/32/36399_2.png) [@eyeHawk](https://discourse.julialang.org/u/eyeHawk)\
**Post date:** [August 2, 2022, 7:53pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/3 "2022-08-02T19:53:31Z")

</div>

It may be the way I am reading the words in

julia\> using DelimitedFiles

julia\> words = readdlm(“test.txt”)  
3×1 Matrix{Any}:  
“Hello”  
false  
“Goodbye”

julia\> for word in words  
push!(data2,lowercase(word))  
end  
ERROR: MethodError: no method matching lowercase(::Bool)  
Closest candidates are:  
lowercase(::T) where T\<:AbstractChar at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/strings/unicode.jl:249  
lowercase(::AbstractString) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/strings/unicode.jl:540  
Stacktrace:  
[1] top-level scope  
@ ./REPL[11]:2

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [August 2, 2022, 7:58pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/4 "2022-08-02T19:58:27Z")

</div>

Please quote code with backticks (```) so it’s easier to read.

The function `readdlm` allows you to pass a type, so I would try this:

```julia
words = readdlm("test.txt", String)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 8:02pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/5 "2022-08-02T20:02:17Z")

</div>

mtelm85’s answer is correct.

```julia
julia> open("test.txt", "w") do io
           println(io, "Hello")
           println(io, "false")
           println(io, "Goodbye")
       end

julia> readdlm("test.txt")
3×1 Matrix{Any}:
      "Hello"
 false
      "Goodbye"

julia> readdlm("test.txt", String)
3×1 Matrix{String}:
 "Hello"
 "false"
 "Goodbye"

julia> lowercase.(ans)
3×1 Matrix{String}:
 "hello"
 "false"
 "goodbye"

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 8:25pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/6 "2022-08-02T20:25:53Z")

</div>

Mark mtelm85’s answer as the solution… I’m just illustrating it.

---

<div class="post-metadata">

**Author:** ![eyeHawk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eyehawk/32/36399_2.png) [@eyeHawk](https://discourse.julialang.org/u/eyeHawk)\
**Post date:** [August 2, 2022, 8:42pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/7 "2022-08-02T20:42:08Z")

</div>

I’m new to Julia so if I may ask. For the purpose I have is readdlm the appropriate function just to read in an array of strings? Also can I do the lowercase operation in-place on original array?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 8:47pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/8 "2022-08-02T20:47:35Z")

</div>

I would have probably done the following given that your words are on separate lines:

```julia
julia> function get_lowercase_lines(io = open("test.txt"))
           lowercase_lines = String[]
           for line in eachline(io)
               push!(lowercase_lines, lowercase(line))
           end
           return lowercase_lines
       end
get_lowercase_lines (generic function with 2 methods)

julia> get_lowercase_lines()
3-element Vector{String}:
 "hello"
 "false"
 "goodbye"

```

`eachline` is a lazy iterator. It does not allocate a `String` array to start.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 8:50pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/9 "2022-08-02T20:50:30Z")

</div>

If you wanted a fully lazy version, consider this version that does not create a `String` array at all:

```julia
julia> function get_lowercase_lines_lazy(io = open("test.txt"))
           return (lowercase(line) for line in eachline(io))
       end
get_lowercase_lines_lazy (generic function with 2 methods)

julia> generator = get_lowercase_lines_lazy()
Base.Generator{Base.EachLine{IOStream}, var"#7#8"}(var"#7#8"(), Base.EachLine{IOStream}(IOStream(<file test.txt>), Base.var"#385#388"(), false))

julia> println.(generator);
hello
false
goodbye
```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 2, 2022, 10:02pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/10 "2022-08-02T22:02:31Z")

</div>

> [@mkitti](#):
>
> `generator = get_lowercase_lines_lazy()`

Could you please enlighten us as to why we can do this:

```julia
collect(get_lowercase_lines_lazy())
3-element Vector{String}:
 "hello"
 "false"
 "goodbye

```

but not `collect(generator)` ?

Thanks.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 10:05pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/11 "2022-08-02T22:05:38Z")

</div>

`collect(generator)` should work. You cannot call it twice though.

```julia
julia> generator = get_lowercase_lines_lazy()
Base.Generator{Base.EachLine{IOStream}, var"#1#2"}(var"#1#2"(), Base.EachLine{IOStream}(IOStream(<file test.txt>), Base.var"#385#388"(), false))

julia> collect(generator)
3-element Vector{String}:
 "hello"
 "false"
 "goodbye"

julia> collect(generator)
String[]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 2, 2022, 10:19pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/12 "2022-08-02T22:19:24Z")

</div>

Sorry if this is a silly question: why is this a one-time use generator when the first form can be collected as many times as we like?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 2, 2022, 10:43pm UTC](https://discourse.julialang.org/t/how-do-i-get-readdlm-to-return-a-matrix-of-strings/85186/13 "2022-08-02T22:43:12Z")

</div>

`collect(get_lowercase_lines_lazy())` creates a new generator every single time. The `eachline` generator will close the file at the end.

If you want to use it repeated, try this:

```julia
julia> function get_lowercase_lines_lazy(filename::String)
           s = open(filename)
           return (lowercase(line) for line in Base.EachLine(s, ondone=()->seekstart(s)))
       end
get_lowercase_lines_lazy (generic function with 3 methods)

julia> generator = get_lowercase_lines_lazy("test.txt")
Base.Generator{Base.EachLine{IOStream}, var"#3#5"}(var"#3#5"(), Base.EachLine{IOStream}(IOStream(<file test.txt>), var"#4#6"{IOStream}(IOStream(<file test.txt>)), false))

julia> collect(generator)
3-element Vector{String}:
 "hello"
 "false"
 "goodbye"

julia> collect(generator)
3-element Vector{String}:
 "hello"
 "false"
 "goodbye"

```
