From: "cardboard42@..." Date: 2007-07-26T00:35:03+09:00 Subject: Re: read variables from string On Jul 25, 11:09 am, daniel wrote: > Hi, > > I would like to extract an array of e.g. integers or floats from a > string: > > s = "1234" -> [12,34] > s = "12.413.423.7" -> [12.4,13.4,23.7] > > How can I use unpack for that? What format string do I need for the > two examples above? > > Where can I find more examples of the unpack method, than in the ruby- > doc.org? > > Thanks for your help > Daniel I don't really know how to use unpack, and I'm not positive it's appropriate for this situation anyway. This works though: def extract str str.scan(/(\d{2})(\.\d)?/).collect { |a| a.join } end extract "1234" => ["12,"34"] extract "12.413.423.7" => ["12.4","13.4","23.7"] My regexp-fu is a little rusty so maybe there's a better way to do that. Bored at work today... Ken