# Problem while printing the content of an array, which is pointed by a variable

**URL:** <https://www.ruby-forum.com/t/problem-while-printing-the-content-of-an-array-which-is-pointed-by-a-variable/134014>\
**Category:** Ruby\
**Created:** [April 6, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/problem-while-printing-the-content-of-an-array-which-is-pointed-by-a-variable/134014 "2008-04-06T18:00:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [April 6, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/problem-while-printing-the-content-of-an-array-which-is-pointed-by-a-variable/134014/1 "2008-04-06T18:00:59Z")

</div>

Team,

I am trying to print the content of array, which is “addressed?” by a  
variable.  
Instead of getting the content of the array, I am getting its name.  
I think it is better to take a look at the code bellow.  
PLEEEEEEEEEEEEEASE  
Do not laugh at my code, I’m still a beginner!

For example, for array @a1h, I expected to see: 1,0,0,0,7,0,0,0,4 but  
instead I see: @a1h.

class SudokuBrute  
def initialize

```
# The following sudoku is from:

```

[http://www.life.com/Life/sudoku/0,26379,,00.html](http://www.life.com/Life/sudoku/0,26379,,00.html)  
# This puzzle is rated as: easy  
@a1h = [1,0,0,0,7,0,0,0,4]  
@a2h = [0,0,0,6,0,4,0,5,0]  
@a3h = [0,0,8,0,9,0,0,0,0]  
@a4h = [6,0,0,9,0,0,0,1,5]  
@a5h = [0,1,9,0,0,0,7,4,0]  
@a6h = [5,3,0,0,0,1,0,0,8]  
@a7h = [0,0,0,0,4,0,3,0,0]  
@a8h = [0,7,0,5,0,8,0,0,0]  
@a9h = [4,0,0,0,6,0,0,0,1]  
end # End method initialize

def pGrid  
for  
x in 1…9 do  
for  
i in 1…9 do  
arr = “@a” + i.to\_s + “h”  
puts arr  
end  
end  
end # End of method pGrid  
end # End Class

class Array  
include Comparable  
end

##################  
ms = SudokuBrute.new

# Output initial settings

ms.pGrid

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [April 7, 2008, 2:44am UTC](https://www.ruby-forum.com/t/problem-while-printing-the-content-of-an-array-which-is-pointed-by-a-variable/134014/2 "2008-04-07T02:44:12Z")

</div>

Stefano,

Thank you for your suggestion and sample code.  
I will use it.

Thanks again,

Victor

On Sun, Apr 6, 2008 at 12:18 PM, Stefano C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [April 6, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/problem-while-printing-the-content-of-an-array-which-is-pointed-by-a-variable/134014/3 "2008-04-06T18:19:18Z")

</div>

On Sunday 06 April 2008, Victor R. wrote:

> ```
> @a5h = [0,1,9,0,0,0,7,4,0]
> i in 1..9 do
> 
> ```
> 
> ##################  
> ms = SudokuBrute.new
> 
> # Output initial settings
> 
> ms.pGrid

Well, it’s doing what you’re asking it to do. arr is not an array, it’s  
a  
string like “@a1h”. To achieve what you want, you can use  
instance\_variable\_get, which returns the content of the instance  
variable  
whose name is passed as argument:

arr = “@a” + i.to\_s + “h”  
puts instance\_variable\_get(arr)

The above suggestion works and only requires a small change in your  
code. Most  
likely, though, you should rethink the design of your class, replacing  
the  
nine instance variables with a single hash or array variable, like this:

@a = {  
:a1h =\> [1,0,0,0,7,0,0,0,4],  
:a2h =\> [0,0,0,6,0,4,0,5,0],  
:a3h =\> [0,0,8,0,9,0,0,0,0],  
:a4h =\> [6,0,0,9,0,0,0,1,5],  
:a5h =\> [0,1,9,0,0,0,7,4,0],  
:a6h =\> [5,3,0,0,0,1,0,0,8],  
:a7h =\> [0,0,0,0,4,0,3,0,0],  
:a8h =\> [0,7,0,5,0,8,0,0,0],  
:a9h =\> [4,0,0,0,6,0,0,0,1]  
}

This stores the content of the grid into a hash, where each entry  
represents a  
row. The keys are symbols (you can replace them with strings, if you  
like) and  
correspond to the names you gave to your instance variables. You can  
access a  
row with:

row = @a[:a3h]

The downside of using a hash is that entries aren’t ordered. If you need  
to  
iterate on all the rows in order, this isn’t the best solution. In this  
case,  
I’d use an array:

@a = [  
[1,0,0,0,7,0,0,0,4],  
[0,0,0,6,0,4,0,5,0],  
[0,0,8,0,9,0,0,0,0],  
[6,0,0,9,0,0,0,1,5],  
[0,1,9,0,0,0,7,4,0],  
[5,3,0,0,0,1,0,0,8],  
[0,0,0,0,4,0,3,0,0],  
[0,7,0,5,0,8,0,0,0],  
[4,0,0,0,6,0,0,0,1]  
]

This way, rows can be accessed sequentially in the correct order, using  
@a.each or the for … in syntax you used above . You can also access  
them  
using the [] syntax:

@a[3]

The downside is that you can no longer associate a key with the items.

I hope this helps

Stefano
