# A newbie question~ about 2 dimension hash? really appreciate~

**URL:** <https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304>\
**Category:** Ruby\
**Created:** [October 4, 2007, 11:41am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304 "2007-10-04T11:41:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 4, 2007, 11:41am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304/1 "2007-10-04T11:41:02Z")

</div>

I want to create a 2 dimension hash, and init it like  
A B C D E  
A 0 0 0 0 0  
B 0 0 0 0 0  
C 0 0 0 0 0  
D 0 0 0 0 0  
E 0 0 0 0 0

then I can set it by ARGV  
for example, if ARGV is AB2, BC5, CD7,AE6,CA2, DB3,DE8, EA3, EC9  
then the expect result is  
A B C D E  
A 0 2 0 0 6  
B 0 0 5 0 0  
C 2 0 0 7 0  
D 0 3 0 0 8  
E 3 0 9 0 0

well, I wrote some codes like this  
#############################################  
ALL=“A”…“E”  
dimension={}  
def init(dime)  
tmp={}  
ALL.each do |letter|  
tmp[letter]=0  
end  
ALL.each do |letter|  
dime[letter]=tmp  
end  
puts “Testing Init”  
ALL.each do |key1|  
ALL.each do |key2|  
print key1+key2+" value="  
puts dime[key1][key2]  
end  
end  
print “Test init is over\n\n”  
end  
def test(array,dime)  
array.each do |arr|  
dime[arr[0].chr][arr[1].chr]=Integer(arr[2].chr)  
end  
puts “Testing”  
ALL.each do |key1|  
ALL.each do |key2|  
print key1+key2+" value="  
puts dime[key1][key2]  
end  
print “\n”  
end  
print “Test is over\n\n”  
end

init(dimension)  
test(ARGV, dimension)  
#############################################  
then the result will be

```
  A B C D E

```

A 3 3 9 7 0  
B 3 3 9 7 0  
C 3 3 9 7 0  
D 3 3 9 7 0  
E 3 3 9 7 0

please tell me why i am wrong?  
I thought the “last” decide the whole volume  
like EC9 make C volume 9  
EA3 make A volume 3  
please help me~  
thank you so much~

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 4, 2007, 11:56am UTC](https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304/2 "2007-10-04T11:56:49Z")

</div>

On 10/4/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> then the expect result is  
> dimension={}  
> ALL.each do |key2|  
> puts “Testing”  
> init(dimension)
> 
> please tell me why i am wrong?  
> I thought the “last” decide the whole volume  
> like EC9 make C volume 9  
> EA3 make A volume 3  
> please help me~  
> thank you so much~

Sorry for not having more time right now but the pattern recalls shared  
objects  
when you do this

c\> dime[letter]=tmp

in init you share all lines and they will always refer to the same  
object.

try this and tell me if that solves your issue

c\> dime[letter]=tmp.dup

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 4, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304/3 "2007-10-04T13:37:45Z")

</div>

2007/10/4, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > for example, if ARGV is AB2, BC5, CD7,AE6,CA2, DB3,DE8, EA3, EC9  
> > ALL=“A”…“E”  
> > ALL.each do |key1|  
> > end
> > 
> > E 3 3 9 7 0  
> > Sorry for not having more time right now but the pattern recalls shared objects  
> > HTH  
> > Robert

I guess you are right with regard to the aliasing effect.

Here’s another solution:

RKlemme@padrklemme1 /cygdrive/c/SCMws/RKlemme  
$ cat ~/ruby/matrix.rb  
#!ruby  
ALL=“A”…“E”  
args = %w{AB2 BC5 CD7 AE6 CA2 DB3 DE8 EA3 EC9}  
matrix = Hash.new 0  
args.each do |a|  
d1, d2, v = a.scan /./  
matrix[[d1, d2]] = v.to\_i  
end

print " “, ALL.to\_a.join(” "), “\n”  
ALL.each do |d1|  
print d1  
ALL.each do |d2|  
print " ", matrix[[d1,d2]]  
end  
print “\n”  
end

require ‘pp’  
pp matrix

RKlemme@padrklemme1 /cygdrive/c/SCMws/RKlemme  
$ ~/ruby/matrix.rb  
A B C D E  
A 0 2 0 0 6  
B 0 0 5 0 0  
C 2 0 0 7 0  
D 0 3 0 0 8  
E 3 0 9 0 0  
{[“A”, “E”]=\>6,  
[“A”, “B”]=\>2,  
[“D”, “B”]=\>3,  
[“C”, “A”]=\>2,  
[“E”, “C”]=\>9,  
[“D”, “E”]=\>8,  
[“C”, “D”]=\>7,  
[“E”, “A”]=\>3,  
[“B”, “C”]=\>5}

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 4, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/a-newbie-question-about-2-dimension-hash-really-appreciate/116304/4 "2007-10-04T13:55:56Z")

</div>

On Oct 4, 5:56 pm, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > then I can set it by ARGV  
> > #############################################  
> > puts “Testing Init”  
> > dime[arr[0].chr][arr[1].chr]=Integer(arr[2].chr)  
> > end  
> > D 3 3 9 7 0  
> > when you do this  
> > Robert  
> > –  
> > what do I think about Ruby?[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

that’s really helpful~  
thank you very much ^O^
