regexp or not

J

Josselin

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
 
D

David Vallner

Josselin said:
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
 
J

Justin Collins

Josselin said:
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
 
T

Thorsten Haude

--U3BNvdZEnlJXqmh+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi,

* Josselin wrote (2006-08-24 04:05):
I get a string person =3D 'Clara Mint" which is a full name
and I would like to find the object "id" in a collection using this =20
full name :

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

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

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


Thorsten French Kicks: Also Ran
--=20
The ability to quote is a serviceable substitute for wit.
- Somerset Maugham

--U3BNvdZEnlJXqmh+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFE7KjcW/x2JEBlodoRArx3AJ9VnjvVltWfD8yIxBqtP/jP/L6MqQCeO8jO
1R8AbcXGhZ5/tQRtyTXMC74=
=+Mj6
-----END PGP SIGNATURE-----

--U3BNvdZEnlJXqmh+--
 
J

James Edward Gray II

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

dblack

Hi --

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

David Vallner

James said:
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
 
R

Robert Klemme

David said:
Josselin said:
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
 
D

David Vallner

Robert said:
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
 
J

Josselin

Hi --

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
 
M

Michael W. Ryder

James said:
Josselin said:
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.
 

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,210
Messages
2,571,091
Members
47,692
Latest member
RolandRose

Latest Threads

Top