[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

extract data from an array

Sijo Kg

3/28/2008 9:47:00 AM

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
--
Posted via http://www.ruby-....

3 Answers

Jesús Gabriel y Galán

3/28/2008 10:00:00 AM

0

On Fri, Mar 28, 2008 at 10:47 AM, Sijo Kg <sijo@maxxion.com> wrote:
> 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

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.

Todd Benson

3/28/2008 10:06:00 AM

0

On Fri, Mar 28, 2008 at 4:47 AM, Sijo Kg <sijo@maxxion.com> wrote:
> 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

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

Sijo Kg

3/28/2008 10:21:00 AM

0

Hi

Thanks for your replies

Sijo
--
Posted via http://www.ruby-....