# Importing strings in Mathematica

This is my first post so apologies if any obvious mistakes.

I'm trying to work on a data containing names (in quotes) in Mathematica. The data is already saved on my system under names.txt. I would like to be able to sort these names in alphabetical order but I can't seem to do anything with it in Mathematica. None of the commands seem to be working and the file itself is not recognized as a string. I'm sure I'm making an obvious mistake in my code but would greatly appreciate any help. Here's my code below:

Original file (reduced version-original file has more than 1000 names): "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON"

data1 = {Import["/Users/Joeblogs/Documents/mathematica/names.txt","string"]}


the output looks like:

{"JOHN", "DAVID", "VIVIAN", "ROBERT"}


How do I sort these in alphabetical order?

-
What if you use "String"? – J. M. Jun 3 '13 at 14:50
Do you mean String (with the "s" in capital)?. I tried that, it's still the same. When I use the "Sort" function, there's no change to the imported data. Thanks for your reply. – John Jun 3 '13 at 14:56
To sort you can use: Sort[{"JOHN", "DAVID", "VIVIAN", "ROBERT"}] – user21 Jun 3 '13 at 14:57
does that work: data1 = StringSplit[StringReplace[Import["names.txt"], "\"" -> ""], ","]? (and then Sort) – Pinguin Dirk Jun 3 '13 at 15:10
@John so, it seems I was right--you are importing the whole lot as a single string. Import as "CSV" instead of "String" and the correct result will be obtained. – Oleksandr R. Jun 3 '13 at 15:21

I think you want to use ReadList here instead of Import as it gives you lower level control. For the sake of the demo, I converted your example to a string, and using StringToStream converted it to an InputStream. In actuality, you can use ReadList directly on the filename.

strm = StringToStream[
"\"MARY\",\"PATRICIA\",\"LINDA\",\"BARBARA\",\"ELIZABETH\", \
"\JENNIFER\",\"MARIA\",\"SUSAN\",\"MARGARET\",\"DOROTHY\",\"LISA\", \
"\NANCY\",\"KAREN\",\"BETTY\",\"HELEN\",\"SANDRA\",\"DONNA\",\"CAROL\", \
\"RUTH\",\"SHARON\""
]


The key is to set the separators in ReadList, so it can do the processing for you:

ReadList[strm, Word, WordSeparators -> {" ", ",", "\t"}]


which returns

{"\"MARY\"", "\"PATRICIA\"", "\"LINDA\"", "\"BARBARA\"",
"\"ELIZABETH\"", "\"JENNIFER\"", "\"MARIA\"", "\"SUSAN\"", "\"MARGARET\"",
"\"DOROTHY\"", "\"LISA\"", "\"NANCY\"", "\"KAREN\"", "\"BETTY\"",
"\"HELEN\"", "\"SANDRA\"", "\"DONNA\"", "\"CAROL\"", "\"RUTH\"",
"\"SHARON\""}

Close[strm]


In your case, replace strm with the filename.

-
thank you rcollyer, your code works very well and it gives the output in string format. Brilliant! Thanks for the explanation too! – John Jun 3 '13 at 15:26
@John ReadList is my usual default for this type of thing. Occasionally, I will use Import, but quite often I deal with formats that aren't supported by it, although I have written my own filters for it before. However, for simple things, I tend not to bother. – rcollyer Jun 3 '13 at 15:32