From: Brian Candler Date: 2009-06-29T21:06:22+09:00 Subject: Re: how to make a variable based on array length and member value Catsquotl wrote: > I have an array full_array[] of an arbitrary number of transaction > objects. > within the transaction is a Date member.. > > From the array I have created a new array years[].uniq! based on the > Date.year members > > what id like to do is take the new arrays members and use them as names > for variables.. > > Say the array holds members for 2007, 2008, 2009 > i`d like to iterate through the full_array and map all members from 2008 > in an array. Sounds like what you want is a Hash, not an Array. Something like: year_to_members = {} members.each do |mem| y = mem.date.year year_to_members[y] ||= [] year_to_members[y] << mem end p year_to_members[2009] # this prints an array containing elememnts where date.year == 2009 As others will probably point out, this pattern can be simplified a bit. But hopefully it's clear enough as a starting point. The fundamental point is, you don't want to create "names of variables" for each year - even though that is technically possible, as others may also point out. The right thing to do is to build a Hash, which is an object which associates a key (a year number) with a value (an array of transaction objects). HTH, Brian. -- Posted via http://www.ruby-forum.com/.