Thanks for your input. But I was looking for something like an inverse
of sprintf (where you can extract variables from a string according to
a given format, instead of gluing a string together from variables).
Does anybody know if something like this exists in ruby?
Thanks,
Daniel
···
On 25 Jul., 17:38, "Harry Kakueki" <list.p...@gmail.com> wrote:
On 7/26/07, daniel <daniel.leuenber...@gmx.ch> 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?
It depends on your rules for extracting data.
You can try something like this.
s = "1234" #-> [12,34]
t = "12.413.423.7" #-> [12.4,13.4,23.7]
p s.scan(/\d{2}/).map{|x| x.to_i}
p t.unpack("a4a4a4").map{|x| x.to_f}
There are probably better ways.
Harry
--
A Look into Japanese Ruby List in Englishhttp://www.kakueki.com/
On 7/26/07, daniel <daniel.leuenber...@gmx.ch> 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?
It depends on your rules for extracting data.
You can try something like this.
s = "1234" #-> [12,34]
t = "12.413.423.7" #-> [12.4,13.4,23.7]
p s.scan(/\d{2}/).map{|x| x.to_i}
p t.unpack("a4a4a4").map{|x| x.to_f}
There are probably better ways.
Harry
--
A Look into Japanese Ruby List in Englishhttp://www.kakueki.com/
Thanks for your input. But I was looking for something like an inverse
of sprintf (where you can extract variables from a string according to
a given format, instead of gluing a string together from variables).
Does anybody know if something like this exists in ruby?
Thanks for your input. But I was looking for something like an inverse
of sprintf (where you can extract variables from a string according to
a given format, instead of gluing a string together from variables).
Does anybody know if something like this exists in ruby?