Using a Regexp to find a Hash Key?

K

Ken Collins

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
 
P

Paul Battley

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.
 
K

Ken Collins

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
 
M

MonkeeSage

Ken said:
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
 
D

dblack

Hi --

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

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 | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
M

MonkeeSage

MonkeeSage said:
How about:

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

Regards,
Jordan
 
K

Ken Collins

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top