# New to Julia / lost in translation / how to index a simple 2d-array of strings

**URL:** <https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537>\
**Category:** New to Julia\
**Tags:** question, strings, arrays\
**Created:** [October 10, 2022, 10:05pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537 "2022-10-10T22:05:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 10, 2022, 10:05pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/1 "2022-10-10T22:05:28Z")

</div>

Hi,

I’ve (very recently) started with julia and decided I would migrate my (java-) chess-engine to julia.

Installation of the environment and ide (vs code) has been pretty smooth and I like working in the repl, which neatly complements my need to get aquainted with lots of new constructs and concepts of the language. I have plenty of experience, in general, hacking basic & 6510-assembler as a preteen (back in the 80s), then turbo-pascal & C(++), java, sql, R and whatever else I had to deal with, in various challenges as a freelancer (even pl/1 on a mainframe for a while). Nowadays, I’m not coding that much, anymore, but am trying to stay up-to-date with the developments on a practical level.

My first goal is to migrate the utility-, debugging- and logging-functions of the engine to julia. It’s the most boring part of it, but essential for a clean migration and it’s also my opportunity to learn the constructs, patterns & tools, julia provides, before I need my brain for more critical (and performance-relevant!) pieces of code, which include _a lot_ of bit-fiddling on integers, processor intrinsics, branch-avoidance, etc…

…however, I’ve already gotten stuck, trying to understand the way, julia indexes into arrays. I know about the column-major indexing, etc. - so one might think “how hard can that even be?”  
…well, turns out: Harder than I thought: 😅

```julia
const DISPLAY_MARKED = "|"
const DISPLAY_NOTMARKED = " "
const DISPLAY_PIECE = "O"
const DISPLAY_NOPIECE = " "
const DISPLAY_SQUARE = [ (DISPLAY_NOTMARKED * DISPLAY_NOPIECE * DISPLAY_NOTMARKED)
                                    (DISPLAY_MARKED * DISPLAY_NOPIECE * DISPLAY_MARKED)
                                ; (DISPLAY_NOTMARKED * DISPLAY_PIECE * DISPLAY_NOTMARKED)
                                    (DISPLAY_MARKED * DISPLAY_PIECE * DISPLAY_MARKED)
                                ]

```

…these are some basic definitions of global constants to be used for pure utility- / debugging-purposes of displaying human-readable bitboards (= 64bit unsigned integers), representing certain aspects of a chess position.

The idea of `DISPLAY_SQUARE` is to generate a very simple constant 2x2 lookup-table (with Strings of length 3), to help with generating strings, being printed on the console (only, when testing / debugging), showing the state of 3 bitboards (3x `UInt64`), projected on _one_ 8x8 chessboard. For example all white pawns (first bitboard) vs. all black pieces (2nd bitboard) and all the squares, the white pawns can move to, including captures (3rd bitboard) of black pieces, like here…

![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/8825658e637c6b1464594b6db5a3ebede3261a89.png)

In the example, I’m expecting to index into `DISPLAY_SQUARE[i1][i2]` with `i1/i2==2`, when there is a piece on a square / a white pawn can move to that square (capturing in case there is a piece) and `...==1`, otherwise. (There are also other use-cases for this debugging-pattern, i.e. 4 possible states of every square depending on 2…3 bitboards = `UInt64`s).

However, it’s not working, like I’m expecting (I fell back to some `if-then-else` logic, for the example, above). This piece of code is not critical (for debugging, only) but the engine-core will have to use _a lot_ of other / different 2d or 3d lookup-tables for performance-critical parts, also (luckily no strings, though, there).

For some reason, I’m ending up with a 1d-array of cardinality 4. What am I missing?!? 🤔  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/7/77522e2a9d6faca5c5c658c1d2c5dc249461f062.png)

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [October 10, 2022, 10:28pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/2 "2022-10-10T22:28:25Z")

</div>

The line breaks between elements in your code mark new rows and thus create a 1d Array, i.e. a Vector. To get a matrix, list the elements in the same row without line breaks, like this

```julia
const MA = "|"
const NM = " "
const PI = "O"
const NP = " "
const SQUARE = [ NM*NP*NM MA*NP*MA ;
                   NM*PI*NM MA*PI*MA]

```

Then

```julia
julia> SQUARE 
2×2 Matrix{String}:
 " " "| |"
 " O " "|O|"

julia> SQUARE[1,2]
"| |"

```

The variations of array concatenation and array literals are explained here: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation)

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [October 10, 2022, 10:49pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/3 "2022-10-10T22:49:52Z")

</div>

I believe the problem here is that a line-break is the same as a `;`, not a space. So while `[1 2; 3 4]` makes a matrix, the code above is like `[1;2;3;4]`.

These array literals are pretty confusing, and it might be clearer to avoid them. Some ways are:

```julia
# product comprehension:
julia> [d*p*d for p in (DISPLAY_NOPIECE, DISPLAY_PIECE), d in (DISPLAY_NOTMARKED, DISPLAY_MARKED)]
2×2 Matrix{String}:
 " " "| |"
 " O " "|O|"

# broadcasting with two rows & one column:
julia> [DISPLAY_NOTMARKED DISPLAY_MARKED] .* [DISPLAY_NOPIECE, DISPLAY_PIECE] .* [DISPLAY_NOTMARKED DISPLAY_MARKED]
2×2 Matrix{String}:
 " " "| |"
 " O " "|O|"

# broadcasting with one row & one column
julia> broadcast([DISPLAY_NOTMARKED DISPLAY_MARKED], [DISPLAY_NOPIECE, DISPLAY_PIECE]) do m, p
         m * p * m
       end
2×2 Matrix{String}:
 " " "| |"
 " O " "|O|"

```

> [@fins](#):
>
> I’m expecting to index into `DISPLAY_SQUARE[i1][i2]`

Note that this won’t work. It will need to be `DISPLAY_SQUARE[i1, i2]` if you have a matrix, as this is distinct from a vector of vectors.

---

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 10, 2022, 10:55pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/4 "2022-10-10T22:55:29Z")

</div>

Thx so much, for the quick response!

However, though it looks good on the surface, I still can’t index into the array in a 2d-way (at least not the way, I was anticipating)…

![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/31bf547c28c0bc40dfae5f9e1906440f9811de8d.png)

…it’s almost, as if the array remained 1d, below the surface (see `SQUARE[1:4]` and the 2nd index is indexing a char-array, underlying each of the 4 strings. 🤷‍♂️

![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/781063cd5788d72a81e4ee4771564388712a2ff8.png)

I have been fiddling with this issue for roughly 3h…4h, now, and I had actually been at this point, once before, but didn’t put that in my original post, as I didn’t understand, how I got there (and lost the 2x2 array on the surface, again).

Your explanation about the line-break made that transparent, luckily - I had looked at the very page you linked, before, but somehow the line-break - detail went under my radar.

How can indexing a 2x2 array be so hard? 🤣

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [October 10, 2022, 10:56pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/5 "2022-10-10T22:56:40Z")

</div>

As mentioned by @mcabbott:

> [@mcabbott](#):
>
> > [@fins](#):
> >
> > I’m expecting to index into `DISPLAY_SQUARE[i1][i2]`
> 
> Note that this won’t work. It will need to be `DISPLAY_SQUARE[i1, i2]` if you have a matrix, as this is distinct from a vector of vectors.

To index into a matrix you need to do Matrix[i1,i2], to index into a 3d array you need to do `array[i1,i2,i3]` etcetera.

You still can index using only one index, as you point out. This is called linear indexing. But doing `Matrix[i1][i2]` is not what you want.

You can take a look at what the docs say about how to index [here](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing).

So if you do `SQUARE[4][2]` you are going to the 4th element of the array (or equivalently the element `[2,2]`). This element is composed of three symbols `|,O, and |`. And you are indexing into the second element of this string, which is the character “O”.

---

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 10, 2022, 11:08pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/6 "2022-10-10T23:08:58Z")

</div>

> Note that this won’t work. It will need to be `DISPLAY_SQUARE[i1, i2]` if you have a matrix, as this is distinct from a vector of vectors.

Ah, that was another unrelated mistake, I made - too much java, in the past, I guess - help much appreciated!

![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2e9bd4e90a5d64856b18c8927c506f25a473af6.png)

…quite interesting, though, how easily julia exposes the underlying memory-alignment and leaves it open for any indexing, I find appropriate - this might come in handy for dealing with large tables and is quite different, from what I’m used to, in java. 👍

---

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 10, 2022, 11:15pm UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/7 "2022-10-10T23:15:09Z")

</div>

```julia
julia> [d*p*d for p in (DISPLAY_NOPIECE, DISPLAY_PIECE), d in (DISPLAY_NOTMARKED, DISPLAY_MARKED)]
2×2 Matrix{String}:
 " " "| |"
 " O " "|O|"

```

…this is my favorite, of your alternatives for defining the array - also much appreciated. Love julia for this kind of syntax, already!

---

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 11, 2022, 2:38am UTC](https://discourse.julialang.org/t/new-to-julia-lost-in-translation-how-to-index-a-simple-2d-array-of-strings/88537/8 "2022-10-11T02:38:48Z")

</div>

Final words…

…didn’t get that variant to work, without an additional function for initializing (or a macro) and went for the other one-liner, as that is possible, en passant, with the declaration of the `SQUARE_FG`-const.

Using arrays (and tuples!) is fun, in julia - quite pleased with the results!

```julia
# =========================================================================================
# lookup-tables for branchless bitboard-display
# SQUARE_FG[i,j] : i = mask, j = piece : each in {1,2} with 1: no, 2: yes
# -----------------------------------------------------------------------------------------
const ML = '[' # mask
const MR = ']' # mask
const NM = ' ' # no mask
const P = 'O' # piece
const NP = ' ' # no piece
const SQUARE_FG = [NM ML] .* [NP;P] .* [NM MR] # 2x2 : forground, no color
const SQUARE_FG_COL = [CRAYON_FG_WHITE CRAYON_FG_BLACK] # 2 : fg-color
const SQUARE_BG_COL = [CRAYON_BG_LIGHT CRAYON_BG_DARK] # 2 : bg-color
# -----------------------------------------------------------------------------------------`

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/4/e4720c2b48ae20522c9746af50d2fab2c7c482b1.png)
