From: sto.mar@... Date: 2013-07-10T14:14:46+09:00 Subject: Re: create variables depending on counter Am 10.07.2013 00:03, schrieb stefan heinrich: > can u say how I have to use the Hash with the headers. I have tried > everything but it doesn't work. That's not a very useful information... You can take my first example: Am 09.07.2013 17:47, schrieb sto.mar@web.de: >> array = ['Car', 'Dog', 'Dog', 'Car', 'Dog'] >> indices = Hash.new {|h,k| h[k] = [] } >> array.each_with_index {|element, index| indices[element] << index } >> indices >> # => {"Car"=>[0, 3], "Dog"=>[1, 2, 4]} >> indices['Car'] >> # => [0, 3] >> indices['Dog'] >> # => [1, 2, 4] assuming your data is given as a 2-dimensional array: data = [ ['Dog', 12], ['Car', 14], ['Car', 67], ['Dog', 98], ['Dog', 32] ] then this would work (first, I create a hash with [] as the default value, that's the trickiest part of the whole thing): values = Hash.new {|h,k| h[k] = [] } data.each {|header, value| values[header] << value } p values['Dog'] # [12, 98, 32] p values['Car'] # [14, 67] Depending on how your data is given, you need to modify the second line. I suggest you also look into the docs for the Hash class and make heavy use of irb. Regards, Marcus --