# Nested iteration in comprehension

**URL:** <https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437>\
**Category:** General Usage\
**Created:** [December 4, 2024, 6:24am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437 "2024-12-04T06:24:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [December 4, 2024, 6:24am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/1 "2024-12-04T06:24:28Z")

</div>

I was expecting the following to read a file into an N x M array of characters:

```julia
load(file) = [c for l in eachline(file), c in l]

```

where the file’s contents are:

```julia
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX

```

However, it seems to return some numbers that I don’t understand instead:

```julia
julia> load("/tmp/example.txt")
10-element Vector{Int64}:
 10112
 10112
 10112
 10112
 10112
 10112
 10112
 10112
 10112
 10112

```

Anyone have any insight into why that happens?

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [December 4, 2024, 6:38am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/2 "2024-12-04T06:38:33Z")

</div>

Only solution I’ve found so far (short of an explicit nested loop) is this:

```julia
load(file) = reduce(vcat, permutedims(collect(l)) for l in eachline(file))

```

which is rather ugly, but does produce the desired result:

```julia
julia> load("/tmp/example.txt")
10×10 Matrix{Char}:
 'M' 'M' 'M' 'S' 'X' 'X' 'M' 'A' 'S' 'M'
 'M' 'S' 'A' 'M' 'X' 'M' 'S' 'M' 'S' 'A'
 'A' 'M' 'X' 'S' 'X' 'M' 'A' 'A' 'M' 'M'
 'M' 'S' 'A' 'M' 'A' 'S' 'M' 'S' 'M' 'X'
 'X' 'M' 'A' 'S' 'A' 'M' 'X' 'A' 'M' 'M'
 'X' 'X' 'A' 'M' 'M' 'X' 'X' 'A' 'M' 'A'
 'S' 'M' 'S' 'M' 'S' 'A' 'S' 'X' 'S' 'S'
 'S' 'A' 'X' 'A' 'M' 'A' 'S' 'A' 'A' 'A'
 'M' 'A' 'M' 'M' 'M' 'X' 'M' 'M' 'M' 'M'
 'M' 'X' 'M' 'X' 'A' 'X' 'M' 'A' 'S' 'X'

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [December 4, 2024, 7:00am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/3 "2024-12-04T07:00:03Z")

</div>

The nested iteration does not allow for depend loops. I believe the rationale behind that is that the nested iteration yields an rectangular `Array` which is not guaranteed if the loops’ lengths can depend on each other.

Here are some other suggestions:

```julia
eachline(file) |> stack |> permutedims
permutedims(stack(eachline(file))) # same as above, just different syntax
stack(eachline(file); dims=1)

```

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [December 4, 2024, 7:03am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/4 "2024-12-04T07:03:18Z")

</div>

Beautiful! Thanks.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [December 4, 2024, 8:07am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/5 "2024-12-04T08:07:48Z")

</div>

Depending on what output you want, you can do:

```julia
load(file) = [c for l in eachline(file) for c in l]

```

and `reshape` the vector to a matrix.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 4, 2024, 8:49am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/6 "2024-12-04T08:49:56Z")

</div>

If you want to save time and the file is formatted cleanly as in the question, using `mmap` will probably be fastest. Also note the use of UInt8 instead of Char, as Char is really a 32-bit Unicode point, but UInt8 is appropriate for old style ASCII only files.

```julia
julia> using Mmap

julia> f = open("/tmp/example.txt")
IOStream(<file /tmp/example.txt>)

julia> A = mmap(f,Matrix{UInt8},(11,10));

julia> M = @view A[1:10,:]
10×10 view(::Matrix{UInt8}, 1:10, :) with eltype UInt8:
 0x4d 0x4d 0x41 0x4d 0x58 0x58 0x53 0x53 0x4d 0x4d
 0x4d 0x53 0x4d 0x53 0x4d 0x58 0x4d 0x41 0x41 0x58
 0x4d 0x41 0x58 0x41 0x41 0x41 0x53 0x58 0x4d 0x4d
 0x53 0x4d 0x53 0x4d 0x53 0x4d 0x4d 0x41 0x4d 0x58
 0x58 0x58 0x58 0x41 0x41 0x4d 0x53 0x4d 0x4d 0x41
 0x58 0x4d 0x4d 0x53 0x4d 0x58 0x41 0x41 0x58 0x58
 0x4d 0x53 0x41 0x4d 0x58 0x58 0x53 0x53 0x4d 0x4d
 0x41 0x4d 0x41 0x53 0x41 0x41 0x58 0x41 0x4d 0x41
 0x53 0x53 0x4d 0x4d 0x4d 0x4d 0x53 0x41 0x4d 0x53
 0x4d 0x41 0x4d 0x58 0x4d 0x41 0x53 0x41 0x4d 0x58

```

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [December 5, 2024, 5:58am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/7 "2024-12-05T05:58:44Z")

</div>

Oh cool, it’s good to know how easy it is to `mmap` files when they’re in clean formats like this.

FWIW this is coming from this year’s [advent of code challenge, day 4](https://adventofcode.com/2024/day/4).

---

<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:** [December 5, 2024, 6:53am UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/8 "2024-12-05T06:53:58Z")

</div>

> [@How to use \`readdlm\` to read in a Matrix{Char}](https://discourse.julialang.org/t/how-to-use-readdlm-to-read-in-a-matrix-char/123464):
>
> Sometimes it’s useful to read in the file as a matrix of characters, in other words, split on empty char – but currently, readdlm(path, dlm::Char) the second argument has to be a Char and there’s no empty Char. am I missing something? The current workaround is not too bad: stack(readlines(path); dims=1)

Might be if interest

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 5, 2024, 12:46pm UTC](https://discourse.julialang.org/t/nested-iteration-in-comprehension/123437/9 "2024-12-05T12:46:16Z")

</div>

[here](https://discourse.julialang.org/t/advent-of-code-2024/123444) another reference to the AOC 2024 context/contest.  
Refer to the ZULIP blog.  
I would have liked to see the discussions about AOC problems here instead.  
I tried my hand at the problem of day 4, I had a lot of difficulty even simply activating the spoiler of my proposed solutions.
