read variables from string

D

daniel

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
 
C

cardboard42

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
 
H

Harry Kakueki

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
 
D

daniel

On 7/26/07 said:
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

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
 
D

dblack

Hi --

On 7/26/07 said:
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

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?

Yes: scanf.

irb(main):001:0> require 'scanf'
=> true
irb(main):002:0> "1234".scanf("%2d%2d")
=> [12, 34]
irb(main):003:0> "12.413.423.7".scanf("%4f%4f%4f")
=> [12.4, 13.4, 23.7]


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,479
Latest member
QFZErin313

Latest Threads

Top