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]