How can i find the location of an item in an array of arrays

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Drachenbauer

I´m working on a sokoban-game:

In the tilemap informations are stored in an array of arrays.
the outer array holds as much inner arrays as my game-screen has tiles in width.
each of the inner arrays has as many positions as my game-screen has tiles in hight.
How can i now find the exact position of a content in this thing?
i want at first a number, in which inner array it is and than the number of the position inside that array.

How do i code this?

The typical way would be to just iterate through the rows and columns with a double for loop looking for the item of interest. Is that not what you want here, or are you unsure how to do that?

jgodfrey | 2022-03-20 21:58

:bust_in_silhouette: Reply From: Wakatta

This all depends on how your arrays are structured.
Arrays have a find method you can stack to get the results you want

var inner_array = outer_array.find(width)
var index = inner_array.find(height)
var value = inner_array[height]

There is simpler way to store and access your data using nested dictionaries

var dict = {{}}
dict[width][height] = value
var value = dict[width][height]

it´s a 2D array

for x in range(grid_size.x):
    for _Y in range(grid_size.y):

I want to receive two numbers


gives the content of the cell, that´s coordinates are given by the Vector2 variable pos.

I want the exact opposite.
i want to give a content (for sample PLAYER) and want to receive it´s coordinates from that grid array.

Drachenbauer | 2022-03-22 18:24

Then double for it is, you’ve practically answered your own question:

for x in range(grid_size.x):
    for y in range(grid_size.y):
        if grid[x][y] == Player:
            return Vector2(x, y)

Wakatta | 2022-03-22 20:47