# Extract data from an array

**URL:** <https://www.ruby-forum.com/t/extract-data-from-an-array/133151>\
**Category:** Ruby\
**Created:** [March 28, 2008, 10:47am UTC](https://www.ruby-forum.com/t/extract-data-from-an-array/133151 "2008-03-28T10:47:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 28, 2008, 10:47am UTC](https://www.ruby-forum.com/t/extract-data-from-an-array/133151/1 "2008-03-28T10:47:01Z")

</div>

Hi  
I have a while loop as below

while count \<= total\_ci\_id\_asso\_types do  
v = “sd\_ci”+"#{count}"  
ci\_id\_asso\_type=params[:"#{v}"]  
count=count+1  
end

for example  
puts ci\_id\_asso\_type gives  
id45service\_desk\_ci\_association\_type\_id1  
id65service\_desk\_ci\_association\_type\_id1 etc

What I need is to get  
45,1  
65,1 (ie two numbers) from above…How can I do that

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 28, 2008, 11:00am UTC](https://www.ruby-forum.com/t/extract-data-from-an-array/133151/2 "2008-03-28T11:00:46Z")

</div>

On Fri, Mar 28, 2008 at 10:47 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts ci\_id\_asso\_type gives  
> id45service\_desk\_ci\_association\_type\_id1  
> id65service\_desk\_ci\_association\_type\_id1 etc
> 
> What I need is to get  
> 45,1  
> 65,1 (ie two numbers) from above…How can I do that

One way, if the string has always those contents, and only the numbers  
change:

x,y =  
ci\_id\_asso\_type.match(/id(\d+)service\_desk\_ci\_association\_type\_id(\d+)/).captures.map  
{|n| n.to\_i}

BTW, I would do the loop like this:

1.upto(total\_ci\_id\_asso\_types) do |count|  
v = “sd\_ci”+“#{count}”  
ci\_id\_asso\_type=params[:“#{v}”]  
end

or

total\_ci\_id\_asso\_types.times do |count|  
v = “sd\_ci”+“#{count + 1}”  
ci\_id\_asso\_type=params[:“#{v}”]  
end

Regards,

Jesus.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 28, 2008, 11:07am UTC](https://www.ruby-forum.com/t/extract-data-from-an-array/133151/3 "2008-03-28T11:07:58Z")

</div>

On Fri, Mar 28, 2008 at 4:47 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts ci\_id\_asso\_type gives  
> id45service\_desk\_ci\_association\_type\_id1  
> id65service\_desk\_ci\_association\_type\_id1 etc
> 
> What I need is to get  
> 45,1  
> 65,1 (ie two numbers) from above…How can I do that
> 
> Thanks in advance  
> Sijo

s = “id45service\_desk\_ci\_association\_type\_id1”  
(s.match /(\d+)service\_desk\_ci\_association\_type\_id(\d+)/)[1,2].join(‘,’)  
=\> “45,1”

Also, instead of params[:“#{v}”], you can just do params[v.to\_sym] or  
params[v.intern]

Todd

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 28, 2008, 11:20am UTC](https://www.ruby-forum.com/t/extract-data-from-an-array/133151/4 "2008-03-28T11:20:49Z")

</div>

Hi

```
 Thanks for your replies

```

Sijo
