Regexp or not

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

Joss

Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

Joss

first, last = person.split(/\s+/)
found_person = people.find { | person |
  person["first"] == first and person["last"] = last
}

Also, do yourself a favour and use a Person class - hashes where the elements have heterogenous meanings make Baby Data Model Design Jesus cry. And index them in a hash by the first name and last name string pair.

David Vallner

Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

Joss

I'd use String#split.

-Justin

Hi,

* Josselin wrote (2006-08-24 04:05):

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this
full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id"
=> "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first"
=> "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last"
=> "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id"
=> "7", "first" => "Che", "last" => "Guevara"]]

Use regexes to get first and last name, then iterate over the
collection. Untested:

person =~ /^(.*) (.*)$/
first = $1
last = $2
people.each do |peop|
    if first == peop["first"] and last == peop["last"]
        print peop["id"]
    end
end

Thorsten French Kicks: Also Ran

···

--
The ability to quote is a serviceable substitute for wit.
    - Somerset Maugham

Hi --

···

On Thu, 24 Aug 2006, Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

I'm wondering whether your data structure is what you want. Right now
you've got an array of arrays of hashes. Are you sure you don't just
want an array of hashes?

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Be warned, parsing names correctly is a non-trivial task...

James Edward Gray II (Hint, hint!)

P.S. My wife's name is Dana Ann Leslie Gray, so don't forget to handle that too. :wink:

P.P.S. I smell Ruby Quiz material... :smiley:

···

On Aug 23, 2006, at 2:16 PM, David Vallner wrote:

Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :
I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :
people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]
any hint to start ?
Joss

first, last = person.split(/\s+/)
found_person = people.find { | person |
  person["first"] == first and person["last"] = last
}

David Vallner wrote:

Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

Joss

first, last = person.split(/\s+/)
found_person = people.find { | person |
    person["first"] == first and person["last"] = last
}

Also, do yourself a favour and use a Person class - hashes where the elements have heterogenous meanings make Baby Data Model Design Jesus cry. And index them in a hash by the first name and last name string pair.

Fully agree. And take care to use the correct number of equal signs in all places. :slight_smile:

Cheers

  robert

Your are right..
well, the problem is maybe less complicated that it seems..
the structure doesn't exist per se, all data are in a DB, I know that the full name is a concatenation of existing data in the DB (first + last) and I need to get the object.
it's an sql problem ( views... ) sorry for raising up a non-ending story ;-))

joss

···

On 2006-08-23 21:55:11 +0200, dblack@wobblini.net said:

Hi --

On Thu, 24 Aug 2006, Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :

I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :

people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]

any hint to start ?

I'm wondering whether your data structure is what you want. Right now
you've got an array of arrays of hashes. Are you sure you don't just
want an array of hashes?

David

James Edward Gray II wrote:

Be warned, parsing names correctly is a non-trivial task...

James Edward Gray II (Hint, hint!)

Silly Merkin people with yer middle names. Why, in my day, we only had one name and we were fine, FINE ya hear me?! We only got them last names after the onions we wore at our belts went out of style... Now geroff my lawn!

P.S. My wife's name is Dana Ann Leslie Gray, so don't forget to handle that too. :wink:

*headschplode*

P.P.S. I smell Ruby Quiz material... :smiley:

Wooerr... I -should- join in the fun one of these days, and that sounds like it might not require *shudder* maths.

David Vallner

Robert Klemme wrote:

first, last = person.split(/\s+/)
found_person = people.find { | person |
    person["first"] == first and person["last"] = last
}

Also, do yourself a favour and use a Person class - hashes where the elements have heterogenous meanings make Baby Data Model Design Jesus cry. And index them in a hash by the first name and last name string pair.

Fully agree. And take care to use the correct number of equal signs in all places. :slight_smile:

Pwnt. *sigh*
(Shh: Ixnay on the bug introduction. All part of the conspiracy.)

David Vallner

You bet you do there are Italian and Hungarien names, which are the other
way round, Spanish names where you can assume that there are two
surnames....
And I do not even know the rules (yet) on Alpha Centauri.

Cheers
Robert

···

On 8/23/06, James Edward Gray II <james@grayproductions.net> wrote:

On Aug 23, 2006, at 2:16 PM, David Vallner wrote:

> Josselin wrote:
>> I am not yet a big friend of reegxp.. and I don't actually know if
>> I should use it or any other function
>> but I'd like to know the easiest path to do the following :
>> I get a string person = 'Clara Mint" which is a full name
>> and I would like to find the object "id" in a collection using
>> this full name :
>> people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"],
>> ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" =>
>> "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first"
>> => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara",
>> "last" => "Mint"], ["id" => "7", "first" => "Che", "last" =>
>> "Guevara"]]
>> any hint to start ?
>> Joss
>
> first, last = person.split(/\s+/)
> found_person = people.find { | person |
> person["first"] == first and person["last"] = last
> }

Be warned, parsing names correctly is a non-trivial task...

James Edward Gray II (Hint, hint!)

P.S. My wife's name is Dana Ann Leslie Gray, so don't forget to
handle that too. :wink:

P.P.S. I smell Ruby Quiz material... :smiley:

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

James Edward Gray II wrote:

···

On Aug 23, 2006, at 2:16 PM, David Vallner wrote:

Josselin wrote:

I am not yet a big friend of reegxp.. and I don't actually know if I should use it or any other function
but I'd like to know the easiest path to do the following :
I get a string person = 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this full name :
people = [ ["id" => "1", "first" => "Jack", "last" => "Johnson"], ["id" => "2", "first" => "Ben", "last" => "Kenneth"], ["id" => "3", "first" => "Ken", "last" => "Olsen"], ["id" => "4", "first" => "Howard", "last" => "Wong"], ["id" => "5", "first" => "Clara", "last" => "Mint"], ["id" => "7", "first" => "Che", "last" => "Guevara"]]
any hint to start ?
Joss

first, last = person.split(/\s+/)
found_person = people.find { | person |
    person["first"] == first and person["last"] = last
}

Be warned, parsing names correctly is a non-trivial task...

James Edward Gray II (Hint, hint!)

P.S. My wife's name is Dana Ann Leslie Gray, so don't forget to handle that too. :wink:

P.P.S. I smell Ruby Quiz material... :smiley:

Working for a collection agency in the SW I found this a very big problem, especially with Hispanic names where you can have the name given to us as Juanita Garcia Rodriguez or worse. And of course we may get it differently from another client. I have to try and split them properly for Credit Reporting and with hundreds of thousands of names it has to be done by computer.