# Read variables from string

**URL:** <https://www.ruby-forum.com/t/read-variables-from-string/110350>\
**Category:** Ruby\
**Created:** [July 25, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350 "2007-07-25T17:11:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 25, 2007, 5:11pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/1 "2007-07-25T17:11:30Z")

</div>

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](http://doc.org)?

Thanks for your help  
Daniel

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 25, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/2 "2007-07-25T17:37:10Z")

</div>

On Jul 25, 11:09 am, daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Where can I find more examples of the unpack method, than in the ruby-  
> [doc.org](http://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

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 25, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/3 "2007-07-25T17:40:28Z")

</div>

On 7/26/07, daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 25, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/4 "2007-07-25T23:48:21Z")

</div>

On 25 Jul., 17:38, “Harry K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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,  
Daniel

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 26, 2007, 12:02am UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/5 "2007-07-26T00:02:57Z")

</div>

daniel wrote:

> 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?

The opposite of printf is scanf:  
[http://hypermetrics.com/rubyhacker/code/scanf/scanf-rdoc/files/EXAMPLES.html](http://hypermetrics.com/rubyhacker/code/scanf/scanf-rdoc/files/EXAMPLES.html)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [July 25, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/read-variables-from-string/110350/6 "2007-07-25T23:56:17Z")

</div>

Hi –

On Thu, 26 Jul 2007, daniel wrote:

> > > two examples above?  
> > > 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?

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
