[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

regexp or not

Josselin

8/23/2006 7:02:00 PM

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


20 Answers

David Vallner

8/23/2006 7:16:00 PM

0

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

Justin Collins

8/23/2006 7:18:00 PM

0

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

Thorsten Haude

8/23/2006 7:18:00 PM

0

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

James Gray

8/23/2006 7:28:00 PM

0

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. ;)

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


dblack

8/23/2006 7:55:00 PM

0

David Vallner

8/23/2006 8:23:00 PM

0

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. ;)

*headschplode*

> P.P.S. I smell Ruby Quiz material... :D

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

8/23/2006 8:49:00 PM

0

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. :-)

Cheers

robert

David Vallner

8/23/2006 9:30:00 PM

0

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. :-)

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

David Vallner

Josselin

8/24/2006 3:12:00 AM

0

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

Michael W. Ryder

8/25/2006 2:16:00 AM

0

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. ;)
>
> P.P.S. I smell Ruby Quiz material... :D
>
>
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.