Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > 2d array of hashes

Reply
Thread Tools

2d array of hashes

 
 
Alex Raduca
Guest
Posts: n/a
 
      05-25-2011
Hi,

I am trying to solve the Sudoku Ruby Quiz using a 2d array of hashes
data model but I probably keep doing something wrong since the hashes
are referenced at row level(last row of the sudoku matrix is
referenced).

Could I get a hint on where I should insert the Hash.new when dealing
with 2d arrays. I have googled it but I could only find simple array of
hashes examples.

Thanks,
Alex



# creates sudoku matrix and fills it in with data
sudoku=Array.new(9,Array.new(9))
(0...each do |x|
(0...each do |y|

#load cell values into :values
sudoku[x][y]=Hash.new
print "load #{temp_ary[x][y]} in sudoku[#{x}][#{y}]\n"
sudoku[x][y][:value]=temp_ary[x][y]

#load possible solutions of this cell into ossibilities
if sudoku[x][y][:value]==0
sudoku[x][y][ossibilities]=[1,2,3,4,5,6,7,8,9]
end
end
end

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      05-25-2011
On Wed, May 25, 2011 at 12:23 PM, Alex Raduca
<(E-Mail Removed)> wrote:
> Hi,
>
> I am trying to solve the Sudoku Ruby Quiz using a 2d array of hashes
> data model but I probably keep doing something wrong since the hashes
> are referenced at row level(last row of the sudoku matrix is
> referenced).
>
> Could I get a hint on where I should insert the Hash.new when dealing
> with 2d arrays. I have googled it but I could only find simple array of
> hashes examples.


So, what is the data model you want? A different hash for each cell?
Then there is a problem here:

> # creates sudoku matrix and fills it in with data
> sudoku=Array.new(9,Array.new(9))


This creates an array of size 9, pointing 9 times to the same array. Check it:

ruby-1.8.7-p334 :001 > a = Array.new(9, Array.new(9))
ruby-1.8.7-p334 :002 > a[0].object_id
=> -611740938
ruby-1.8.7-p334 :003 > a[1].object_id
=> -611740938

So, if you add a hash to that inner array, all your rows will have the
same hash.
Try this:

sudoku = Array.new(9) { Array.new(9)}

or if you want to assign an empty hash right away:

sudoku = Array.new(9) { Array.new(9) {{}}}

now, each position in the array of arrays has its own independent hash:

ruby-1.8.7-p334 :003 > sudoku[0][0][ossibilities] = [1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
ruby-1.8.7-p334 :004 > sudoku
=> [[{ossibilities=>[1, 2, 3, 4, 5, 6, 7, 8, 9]}, {}, {}, {}, {},
{}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {}, {}], [{}, {}, {},
{}, {}, {}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {}, {}], [{},
{}, {}, {}, {}, {}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {}, {}],
[{}, {}, {}, {}, {}, {}, {}, {}, {}], [{}, {}, {}, {}, {}, {}, {}, {},
{}], [{}, {}, {}, {}, {}, {}, {}, {}, {}]]

Jesus.

 
Reply With Quote
 
 
 
 
Alex Raduca
Guest
Posts: n/a
 
      05-25-2011
to be more specific...

...I am creating 81 different new Hashes in the nested loops which I
verified to be true but when checking sudoku[0..8][y] outside the loop
they all have the same object_id

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alex Raduca
Guest
Posts: n/a
 
      05-25-2011
Hi Jesus,

Thanks for the help. Indeed, I didn't realize I was instantiating the 2d
array the wrong way.

Cheers,
Alex

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Hashes in an array of hashes - Complicated! Matt Brooks Ruby 16 09-16-2009 05:53 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments