Using a Regexp to find a Hash Key?

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.arachnoid.com

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.arachnoid.com

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.

···

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

Hi --

···

On Wed, 4 Oct 2006, Ken Collins wrote:

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

I'm having trouble following this thread, because some message bodies
aren't appearing -- but the basic answer is: /.../ is itself a
potential hash key (since keys can be any object), so you have to do
something other than just hand it off to has_key? if you want to
search for a string key that matches it.

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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.

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

MonkeeSage wrote:

How about:

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

Regards,
Jordan

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