Ryan Hall Ryan Hall - 1 year ago 82
Ruby Question

Using Ruby: Why is my array printing the results then an array of with idex numbers

I have two methods:

pwsplayerindex finds a person that is in an array multiple times and gives the index of where their name is.

statarray uses the index # from pwsplayerindex to find other data in an array with hashes.

def pwsplayerindex(inplayer)
arr = Array.new
pwsarr.each_with_index do |val,index|
if val['player'] == inplayer then arr << index end
end
arr
end

def statarray(index,stat)
indexarr = Array.new
pwsplayerindex((pwsarr[index]['player'])).each { |x| puts (pwsarr[x][stat])}
end

print statarray(0,'play')


why does the result yield:

51.0
29.9
29.4
28.1
24.6
16.6
[0,82,88,113,192,472]


All I want it to give me is:

51.0
29.9
29.4
28.1
24.6
16.6


Thank you in advance!

Answer Source
puts statarray(0,'play')

should be just

statarray(0,'play')

otherwise you print the return value of the function, which is the entire array (because the each method)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download