V
vivalon
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~
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~