Multi dimension array

a=[[“doc1”,[“hi”,“bye”,“guy”],[],[],[]],[“doc2”,[“bye”,“now”,“is”,“hi”,“hi”,“hi”],[],[],[]],[“doc3”,[“bye”,“now”,“is”,“hi”,“hi”,“hi”,“why”,“go”,“guy”],[],[],[]]]

i have this array i’d like to multiply the 2nd inner array with the 3rd
inner array and fill the 4th inner array with these results but i don’t
know how to do it. by the way the 2nd and the third inner array are of
type float.
anybody ca help please?
and thanks in advance:)

a.each{
|x|
x.each{
|y|
y.each{ #i have the error in here

    }

}
}

and the error is
tester.rb:50: undefined method each' for 1:Fixnum (NoMethodError) from tester.rb:48:ineach’
from tester.rb:48
from tester.rb:46:in `each’
from tester.rb:46

The error indicates that you are calling “each” method on a Fixnum
object.
You can just do a simple verification inside the each loop like this:

array.each do |x|
do something unless x.class == “Array”
do something else with inner arrays
end

2011/10/25 juman s. [email protected]

juman s. wrote in post #1028453:

a=[[“doc1”,[“hi”,“bye”,“guy”],[],[],[]],[“doc2”,[“bye”,“now”,“is”,“hi”,“hi”,“hi”],[],[],[]],[“doc3”,[“bye”,“now”,“is”,“hi”,“hi”,“hi”,“why”,“go”,“guy”],[],[],[]]]

i have this array i’d like to multiply the 2nd inner array with the 3rd
inner array and fill the 4th inner array with these results

hi Juman,

if we write out your array a like this:

a = [

[“doc1”,
[“hi”,“bye”,“guy”],
[],
[],
[]
],

[“doc2”,
[“bye”,“now”,“is”,“hi”,“hi”,“hi”],
[],
[],
[]
],

[“doc3”,
[“bye”,“now”,“is”,“hi”,“hi”,“hi”,“why”,“go”,“guy”],
[],
[],
[]
]

]

…what sections are you referring to as the 2nd, 3rd, and 4th inner
arrays?

  • j

On Wed, Oct 26, 2011 at 9:13 AM, Robert K.
[email protected] wrote:

a.each{

and the error is
tester.rb:50: undefined method each' for 1:Fixnum (NoMethodError) from tester.rb:48:ineach’
from tester.rb:48
from tester.rb:46:in `each’
from tester.rb:46

Why not use any of these: http://raa.ruby-lang.org/search.rhtml?search=matrix ?

PS: And there’s also http://rdoc.info/stdlib/matrix/1.9.2/frames in
the standard library. For fast math there’s also narray gem.

Cheers

robert

On Tue, Oct 25, 2011 at 10:40 PM, juman s. [email protected] wrote:

|x|
and the error is
tester.rb:50: undefined method each' for 1:Fixnum (NoMethodError) from tester.rb:48:ineach’
from tester.rb:48
from tester.rb:46:in `each’
from tester.rb:46

Why not use any of these:
http://raa.ruby-lang.org/search.rhtml?search=matrix ?

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs