[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using a Regexp to find a Hash Key?

Ken Collins

10/4/2006 4:07:00 AM



9 Answers

Paul Lutus

10/4/2006 4:16:00 AM

0

Ken Collins wrote:

>

This is possible, but it is rather cumbersome and slow, since the regexp has
to be applied to each hash key in turn until a match is found.

Maybe you could provide more detail about the problem to be solved instead
of this notion about how to solve it. Even one word more.

--
Paul Lutus
http://www.ara...

Ken Collins

10/4/2006 11:59:00 AM

0



Ken Collins

10/4/2006 12:34:00 PM

0


I sure could...

The hash is going to be real small. It would at most only have 3 or 4
keys. The one I want has a prefix of an arbitrary string with any
amount of number after it. I need to know if this key is present so
that I can run a conditional block of code. It would be a bonus to
get some match data back on that last series of digits in the regexp.

I would like to use something like this:

def foobar
if hash.has_key?(/^foo_\d+$/)
...
end
end

Meanwhile my code looks like this and I was pretty sure there might
be a more terse solution.

def foobar
unless params.reject{ |k, v| k !~ /^foo_\d+$/ }.empty?
...
end
end


On Oct 4, 2006, at 12:20 AM, Paul Lutus wrote:

> Ken Collins wrote:
>
>>
>
> This is possible, but it is rather cumbersome and slow, since the
> regexp has
> to be applied to each hash key in turn until a match is found.
>
> Maybe you could provide more detail about the problem to be solved
> instead
> of this notion about how to solve it. Even one word more.
>
> --
> Paul Lutus
> http://www.ara...
>


Paul Battley

10/4/2006 12:39:00 PM

0

On 04/10/06, Ken Collins <ken@metaskills.net> wrote:
>
>
>

Very postmodern!

(Does anyone get any body text in Ken's emails? I'm receiving just the
headers - and it looks like I'm not alone, judging by Paul Lutus's
earlier message.)

Paul.

Ken Collins

10/4/2006 12:41:00 PM

0


I sent an new one out right after this.
It appears that my public email key being attached to the email was
wiping the body for some reason.

- Ken (to reply again)



I sure could...

The hash is going to be real small. It would at most only have 3 or 4
keys. The one I want has a prefix of an arbitrary string with any
amount of number after it. I need to know if this key is present so
that I can run a conditional block of code. It would be a bonus to
get some match data back on that last series of digits in the regexp.

I would like to use something like this:

def foobar
if hash.has_key?(/^foo_\d+$/)
...
end
end

Meanwhile my code looks like this and I was pretty sure there might
be a more terse solution.

def foobar
unless params.reject{ |k, v| k !~ /^foo_\d+$/ }.empty?
...
end
end




On Oct 4, 2006, at 8:38 AM, Paul Battley wrote:

> On 04/10/06, Ken Collins <ken@metaskills.net> wrote:
>>
>>
>>
>
> Very postmodern!
>
> (Does anyone get any body text in Ken's emails? I'm receiving just the
> headers - and it looks like I'm not alone, judging by Paul Lutus's
> earlier message.)
>
> Paul.
>


MonkeeSage

10/4/2006 12:54:00 PM

0

Ken Collins wrote:
> def foobar
> unless params.reject{ |k, v| k !~ /^foo_\d+$/ }.empty?
> ...
> end
> end

Hi Ken,

How about:

def foobar(params)
if params.select { |k,v| k =~ /^foo_(\d+)$/ }
p $1
end
end

Regards,
Jordan

dblack

10/4/2006 1:13:00 PM

0

MonkeeSage

10/4/2006 1:23:00 PM

0

MonkeeSage wrote:
> How about:

Actually, make that #find rather than #select, so that iteration stops
as soon as the match goes through.

Regards,
Jordan

Ken Collins

10/5/2006 12:04:00 AM

0


Actually, that was very good advice, THANK YOU!
I did not even consider using an Enumerable method, I was stuck on
Hash methods.

if foo_key = hash.find { |k,v| k =~ /^foo_(\d+)$/ }[0]
# Now I can use foo_key to look at the values
# I can also use $1 where needed for the match
end


Thanks again,
Ken



On Oct 4, 2006, at 9:25 AM, MonkeeSage wrote:

> MonkeeSage wrote:
>> How about:
>
> Actually, make that #find rather than #select, so that iteration stops
> as soon as the match goes through.
>
> Regards,
> Jordan
>
>