tags:

views:

99

answers:

2

I have a big 2-dimensional array A, and also a flat array B of two elements. How can I quickly access element from the A array using numbers (coordinates) in B? The only thing I can do now is:

A[B[0],B[1]]

But the path to these actual arrays through the names of members of my class is too long and dirty, and the actual array names are too long... so I wonder if its possible to ease the job.

A: 

What about turning A into a Hash with two-element arrays as keys? So where you now have something like this:

A = [["TopL","TopR"],["CenterL","CenterR"],["BottomL","BottomR"]]
B = [[0,1],[1,0],[2,1]]
A[B[x][0]][B[x][1]]

You'd instead have:

A = {[0,0] => "TopL", [0,1] => "TopR", [1,0] => "CenterL", [1,1] => "CenterR", [2,0] => "BottomL", [2,1] => "BottomR"}
B = [[0,1],[1,0],[2,1]]
A[B[x]]

Dunno if that'll help in your actual situation, but maybe it'll give you some ideas.

glenn mcdonald
A: 
x = B[0]
y = B[1]
A[x][y]
glebm
If you like this way, the first two lines can be further collapsed into `x,y = B`!
glenn mcdonald