Message Boards Message Boards

0
|
4249 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Search for words in an association?

Posted 6 years ago

I have been playing around with associations to see if I can understand something about the database capabilities in Mathematica. I generated an example dataset consisting of names and email addresses as shown below.

zAssoc={<|"Name" -> "Janice Baker Cody", 
  "Email" -> "janxxx@sbcglobal.net"|>, <|"Name" -> "Jan Scott", 
  "Email" -> "Unpublished"|>, <|"Name" -> "Jim Barbar", 
  "Email" -> "jb@gmail.com"|>, <|"Name" -> "Jean Bates Williams", 
  "Email" -> "jbw123@att.net"|>, <|"Name" -> "John Black", 
  "Email" -> "john.black@sbcglobal.net"|>, <|"Name" -> "Bobby Chance",
   "Email" -> "bobchance@comcast.net"|>, <|"Name" -> "Bob Colt", 
  "Email" -> "unpublished"|>, <|"Name" -> "Jim Davis", 
  "Email" -> "jd55@att.net"|>, <|"Name" -> "Tom Davis", 
  "Email" -> "tdavis@aol.com"|>, <|"Name" -> "Tom Good", 
  "Email" -> "tgood@aol.com"|>, <|"Name" -> "Maryann Williams Bast", 
  "Email" -> "maryann@att.net"|>, <|"Name" -> "Mary Louise Franklin", 
  "Email" -> "mlf@gmail.com"|>, <|"Name" -> "Jimmie Ann Holt", 
  "Email" -> "jah@yahoo.com"|>, <|"Name" -> "Glenda McClaskey Brown", 
  "Email" -> "fcb3336@yahoo.com"|>, <|"Name" -> 
   "Donna Marshall Tartine", 
  "Email" -> "donnatart12@att.net"|>, <|"Name" -> 
   "Janice Moore Taylor", 
  "Email" -> "Janice@gmail.net"|>, <|"Name" -> 
   "Mary Kate Hepworth Donaldson", 
  "Email" -> "mkhd@swbell.net"|>, <|"Name" -> "Don Stevens", 
  "Email" -> "dsteve@gmail.com"|>}

I want to be able to enter either a person's first or last name and find all records corresponding to the name. The following code does this, but it also finds records for persons whose partial name also matches for the desired name.

Do[selName = InputString["name to search for"];
 lenName = StringLength[selName];
 If[lenName == 0, Break[]];
 selRslt = 
  Query[Select[StringContainsQ[#Name, selName] == True &], {"Name", 
     "Email"}]@zAssoc;
 Print[Dataset[selRslt]], 5]

I was able to avoid this by splitting the name field up with the following code but it involved a rather convoluted calculation. My question is whether there is a more direct way of looking for specific words using Mathematica's built-in database capabilities.

Do[selName = InputString["name to search for"];
 lenName = StringLength[selName];
 If[lenName == 0, Break[]];
 selRslt = 
  Query[Select[StringContainsQ[#Name, selName] == True &], {"Name", 
     "Email"}]@zAssoc;
 selNameString = Query[All, "Name"]@selRslt;
 separateWord = 
  Map[StringMatchQ[StringSplit[#], selName] &, selNameString];
 desiredNameLocs = 
  Flatten[Position[AnyTrue[#, TrueQ] & /@ separateWord, True]];
 Print[Dataset[selRslt[[desiredNameLocs]]]], 5]
POSTED BY: Mike Luntz
3 Replies
Posted 6 years ago

Many thanks to Gianluca for a direct answer to my question and to Seth for the link to his tutorial on association and datasets. I had seen the tutorial by Seth and it, in fact, was what got me interested in what could be done in Mathematica relative to databases. I just had not absorbed all the information in his post.

Seth, you are correct in your sense that my background is in languages other than Mathematica. I first started programming in Fortran and then switched to Matlab after it was commercially available.

POSTED BY: Mike Luntz

I get the sense you are coming from a background in other programming languages. Immodestly, you might also want to look at this http://community.wolfram.com/groups/-/m/t/1167544 for general advice on querying data in Mathematica.

POSTED BY: Seth Chandler

You can try with StringSplit and MemberQ:

zDataset = Dataset[zAssoc];
zDataset[Select[MemberQ[StringSplit[#Name], "Tom"] &]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract