kazaam wrote: > I have a file with many entries and much of these I don't need. Let's > imagine: > > name=arthur > age=16 > hobby=swimming > job=dancer > home=DE > name=marry > age=49 > hobby=singing > job=nurse > home=US > [...] > > this is my file and I need just the name,hobby and home of each one. If the order is guaranteed, it's as simple as str.scan(/name=(.+?)\n.*?hobby=(.+?)\n.*?home=(\w+?)/m) > So I need some kind of an array struct which let's me > iterate over each person and let's me use their name,hobby and home values. > But how to achieve this? With with variable-typ and how to get the > correctly collected? You don't need "some kind of array struct" (whatever that would be) - just an array (which may of course contain structs). > I wanna talk to them like this: > > person.each |entry| do > puts entry.name > puts entry.hobby > puts entry.home > end Person=Struct.new(:name, :hobby, :home) re=/name=(.+?)\n.*?hobby=(.+?)\n.*?home=(\w+)/m persons=File.read(file).scan(re).map { |arr| Person.new(*arr) } persons.each do |pers| puts pers.name puts pers.hobby puts pers.home end A more general solution that reads in all information about a person (even if you don't need it): persons=File.read(file).scan(/name=.*?(?=name=|\Z)/m).map do |pers| Hash[*pers.scan(/^(.*)=(.*)$/).flatten] end persons.each do puts pers["name"] puts pers["hobby"] puts pers["home"] end This solution doesn't require the order to be fixed, except that name= has to be the first entry for any person. Another solution that doesn't require any ordering in the file at all: str=File.read(file) Person=Struct.new(:name, :hobby, :home) names=str.scan(/name=(.*)$/).flatten hobbies=str.scan(/hobby=(.*)$/).flatten homes=str.scan(/home=(.*)$/).flatten persons=names.zip(hobbies,homes).map { |pers| Person.new(*pers) } persons.each do |pers| puts pers.name puts pers.hobby puts pers.home end HTH, Sebastian -- NP: Mysticum - Mourning Jabber: sepp2k / jabber.org ICQ: 205544826