How to set/access 2D list/array

Hey everyone,
My inability to answer this question is probably a sign I have been coding too long today. Two-part question. How do you set the value to a 2D list after it has been initialized? And how do you access something from a list inside another list? It’s easy to do in a written language, but I’m struggling over here on how to put it in blocks.

What do you mean by “set the value to a 2D list”? You can only set list item values. So are you asking how to add a list as a list item?

I have a feeling using JSON (and perhaps Firebase) is going to be easier than trying to use two-dimensional lists but I don’t have a ton of experience with them.

2 Likes

I know it sounds odd, but from a written programming perspective, it makes sense :joy:. It does essentially make a table. The first list block is the rows, and the lists inside the list are the columns. Here is what I am trying to do (example), and my goal is to get the 5.


List

Now in a writing language like Java, I can do something like this to get the same result.:

int[][] Array = new int{{1,2,3}, {4,5,6}, {7,8,9}};

To get the array value, you can just grab the value.

int val = Array[2][2];

Or if it is an ArrayList, you use .get notation. So something like. (Syntax idea, not perfect):

int val = Array.get(2).get(2);

So I need to get the first value(row of list), and then get the value from the column (Inside list)

1 Like

Check this
https://x.thunkable.com/projectPage/60ffb90e59eff60011a5a85b

I’m not sure if this is what you mean but if we start with something we can always develop it further.

I appreciate the explanation. Very clear! I actually did understand the concept but I wasn’t sure what you were initially asking. Looks like you figured out how to set the value of the two-dimensional array/list but you’re wondering how to reference items of a list inside a list.

Building off of what @muneer created, this will give you the value of Array[2][2]:

I’d probably create a function with inputs for the row & column to simplify the process and allow you to use it repeatedly:

And here’s yet another method, perhaps the simplest:

Note that when using the get property of object block, the sub-list/array index value starts at 0 unlike the in list get # block which starts at 1. So you’d need to use [2].1 to get the second value of 5.

In case you want to play around with these:

https://x.thunkable.com/copy/c1d27648ec0a62e062df28d6c0a68ca6

3 Likes

You can even embed further (list of list of list). The syntax just gets a little tricky with the brackets’ placement. This will give you the value 32:

Note that the index starting values are either 0 or 1 depending on whether you’re referencing the deepest (I’m sure there’s better terminology than that…) list or not.

So for example, [1].[3].1 references [first list item]–>[third list item].second list item

2 Likes

I think you’ll like this documentation about Nested Values and Values from Arrays:

1 Like

Thanks for the insight! I think this is exactly what I needed. I especially liked that you were able to go even deeper using the object properties. I figured it was going to be using something like two get blocks, but my brain was just having such a hard time fathoming what to do :rofl:. I’ll make this solved. Thanks again everyone!

2 Likes

I find it funny how the indexing changes. Again, I’m usually written coder, so the fact that for most of Thunkable I’m using row one instead of row zero for the first row drives me insane!

1 Like

Yep, the indexing is inconsistent. I think a big part of that is that the get property of object block only just recently gained the ability to use indexing values and I think it was a bit of an afterthought (although a very useful one!).

Things like that can be added to the GitHub Feature Request/Bug section. It’s more likely they’ll be addressed when added there than when mentioned here.

There are trade-offs when making a block-based coding platform. I think they made the right choice as far as list indexing with blocks goes. I think Scratch works the same way. I can only imagine how many people would be creating topics asking “why is my list item #1 showing the value of the second list item???” otherwise. But it can certainly throw off an experienced coder.

3 Likes