(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 32971, 1160]*) (*NotebookOutlinePosition[ 34062, 1195]*) (* CellTagsIndexPosition[ 34018, 1191]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData["8. Strings; Strings as Lists"], "Title", Evaluatable->False, AspectRatioFixed->True], Cell["Last revision: January 26 1998", "SmallText", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "The topic for this lab is the data type String, together with some \ functions ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " provides for manipulating strings. We'll also look at the (data type) \ conversion functions \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["FromCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["StringJoin[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Characters[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", and explore how we can use the list functions we know to duplicate the \ string operations. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" operations/structures introduced: \n", Evaluatable->False, AspectRatioFixed->True], StyleBox[ " StringLength[]\n FromCharacterCode[]\n ToCharacterCode[]\n \ \\\" \\\\ \\n \\t \\b\n StringJoin[]\n StringReverse[]\n \ StringTake[]\n StringDrop[]\n $StringOrder\n DigitQ[]\n \ LetterQ[]\n UpperCaseQ[]\n LowerCaseQ[]\n ToUpperCase[]\n \ ToLowerCase[]\n Characters[]\n <>", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"] }], "SmallText", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell[TextData["Strings and Things"], "Section", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell[TextData["The Data Type String"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "The data type String is used for values which are finite sequences of \ characters, such as \"Jane\" or \"Hello, Mike\". Inside a standard computer \ system, each character of a string is represented by an integer code between \ 0 and 127, each integer between 0 and 127 corresponding to some letter, \ number, or \"special character\" such as \"+\", \"&\", or \"@\". The \ standard code is ASCII, the American Standard Code for Information \ Interchange. (On computer systems providing larger character sets, such as \ sets including vowels with accents, the maximum integer may exceed 127.)"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "To define or use a String does not, however, require you to know the codes \ for each of the characters. The usual way to denote a String is simply to \ write out the list of characters, one at a time (with no spaces in between \ unless you mean them), placing a double quotation mark before the first and \ after the last; for example, \"", StyleBox["Frayed Knot", "Input", FontWeight->"Plain"], "\" is a String with 11 characters: \"", StyleBox["F", "Input", FontWeight->"Plain"], "\", \"", StyleBox["r", "Input", FontWeight->"Plain"], "\", \"", StyleBox["a", "Input", FontWeight->"Plain"], "\", \"", StyleBox["y", "Input", FontWeight->"Plain"], "\", \"", StyleBox["e", "Input", FontWeight->"Plain"], "\", \"", StyleBox["d", "Input", FontWeight->"Plain"], "\", \"", StyleBox[" ", "Input", FontWeight->"Plain"], "\" (the blank space), \"", StyleBox["K", "Input", FontWeight->"Plain"], "\", \"", StyleBox["n", "Input", FontWeight->"Plain"], "\", \"", StyleBox["o", "Input", FontWeight->"Plain"], "\", and \"", StyleBox["t", "Input", FontWeight->"Plain"], "\". Let's get ", StyleBox["Mathematica", FontSlant->"Italic"], " to agree with us:" }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["s1 = \"Frayed Knot\"", "Input", AspectRatioFixed->True], Cell["Head[s1]", "Input", AspectRatioFixed->True], Cell["StringLength[s1]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Notice that \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Length[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" applies to a ", Evaluatable->False, AspectRatioFixed->True], StyleBox["List", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[", \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["StringLength[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" to a ", Evaluatable->False, AspectRatioFixed->True], StyleBox["String", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[", and recall that the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Head[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" function gives us the data type of any name in ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[".", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["ASCII Codes"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["How can we find out the codes ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " uses to represent the letters we see in a String? We could consult a \ table, but instead we'll just ask ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ ". The function we want to use is a data type conversion function, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". If we give a String to \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", it will return a List of the Integer ASCII codes corresponding to each \ character in the String, in order. For instance:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["ToCharacterCode[s1]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "By investigating the codes, you can find that the blank space \"", Evaluatable->False, AspectRatioFixed->True], StyleBox[" ", "Input", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier", FontWeight->"Plain"], StyleBox["\" has code 32, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["a", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox["\" through \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["z", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox["\" have codes 97 to 122, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["A", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox["\" through \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Z", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[ "\" have codes 65 to 90, and \"0\" through \"9\" have codes 48 to 57. You \ won't need to memorize these codes, but it is reassuring to know that the \ code for \"b\" is one greater than the code for \"a\", and so on.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "What if we have the codes, and we would like to form a string of \ characters from the codes? We need the opposite function to \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which is \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["FromCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["FromCharacterCode[32]", "Input", AspectRatioFixed->True], Cell["FromCharacterCode[{49,57,57,54}]", "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Special Characters"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "There are some special characters in ASCII---the characters which instruct \ the computer to begin a new line or to tab over some spaces, for instance. \ Also, there are characters to put a double quotation mark in a ", StyleBox["String", "Input", FontWeight->"Plain"], "; we need such a character, because ", StyleBox["Mathematica", FontSlant->"Italic"], " would have difficulty deciding whether \"\"\"\" is two ", StyleBox["String", "Input", FontWeight->"Plain"], "s with no characters in them, one String with two double quotation marks, \ or one ", StyleBox["String", "Input", FontWeight->"Plain"], " with one double quotation mark alongside an extraneous double quotation \ mark. Here's a partial list of special characters:" }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["\\\" ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["denotes a double quotation mark\n", Evaluatable->False, AspectRatioFixed->True], StyleBox["\\\\ ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["denotes a backslash\n", Evaluatable->False, AspectRatioFixed->True], StyleBox["\\n ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["denotes a forced new line\n", Evaluatable->False, AspectRatioFixed->True], StyleBox["\\t ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["denotes a tab (implemented differently on different systems)\n", Evaluatable->False, AspectRatioFixed->True], StyleBox["\\b ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["denotes a backspace", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["We'll use several of these in one String:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ \"Here's a quotation mark: \\\", a backslash: \\\\, \\nand a new \ line!\"\ \>", "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["String Functions"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "There are many string functions which behave almost exactly like the \ corresponding list functions. Here is a list of a few:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "StringLength[]\nStringJoin[]\nStringReverse[]\nStringTake[]\nStringDrop[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[ "In fact, all of these functions can be defined using list operations along \ with some data type conversion functions. (This is your homework!) Let's \ make sure we know what each of them does:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["StringJoin[\"Davidson\",\"College\"]", "Input", AspectRatioFixed->True], Cell["StringReverse[\"Wildcats\"]", "Input", AspectRatioFixed->True], Cell["StringTake[\"Wildcats\",4]", "Input", AspectRatioFixed->True], Cell["StringDrop[\"Wildcats\",4]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["We can also use the boolean operator \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["==", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" with Strings to test equality. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\"austen\" == \"Austen\"", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Although we cannot use the operators \"<\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox[">", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" on Strings, we can at least sort a List of several Strings using \ \"Sort[]\". How are Strings ordered? It depends on a certain ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" \"environment variable\" called \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["$StringOrder", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\". By default, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["$StringOrder", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is ", Evaluatable->False, AspectRatioFixed->True], StyleBox[ "{{\"a\",\"A\"}, {\"b\",\"B\"}, ...{\"z\",\"Z\"}, \"0\", \"1\", ... \"9\"}", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ ", and it is used to order strings by comparing characters left-to-right. \ If the beginning letters are different, then the \"smaller\" string is \ defined to be the one whose first letter occurs first in ", Evaluatable->False, AspectRatioFixed->True], StyleBox["$StringOrder", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[". If the two first characters are the same, then ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " looks at the two second characters, and so on. Here are some examples:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Sort[{\"Abe\",\"Lincoln\"}]", "Input", AspectRatioFixed->True], Cell["Sort[{\"Jane\",\"jane\"}]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "For those who would like Strings ordered strictly according to their ASCII \ codes, there are two options: either reset \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["$StringOrder", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" to \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["{}", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" (by evaluating \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["$StringOrder = {}", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\"), or decompose the Strings into Integer codes, sort those lists of \ numbers, and put the Strings back together.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "There are some other boolean operators we can use on Strings to test \ various properties.\n\"", Evaluatable->False, AspectRatioFixed->True], StyleBox["DigitQ[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" tests whether or not all of the characters are digits, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["LetterQ[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" whether they are letters, \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["UpperCaseQ[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" whether they are upper-case letters, and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["LowerCaseQ[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" whether they are lower-case letters. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["DigitQ[\"01\"]", "Input", AspectRatioFixed->True], Cell["DigitQ[\"0A\"]", "Input", AspectRatioFixed->True], Cell["UpperCaseQ[\"HELLO\"]", "Input", AspectRatioFixed->True], Cell["LowerCaseQ[\"xxx\"]", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "Finally, we can change the case of characters in a String by using \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToUpperCase[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\", which forces all of the letters in the String to be upper-case, or \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToLowerCase[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which does the opposite.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["ToUpperCase[\"e. e. cummings\"]", "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Strings and Lists"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "We've already encountered functions which change Strings into Lists and \ back again, namely \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["ToCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["FromCharacterCode[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\". Suppose, however, that we don't want to see the codes but simply want \ the String broken up into a list of characters which we can then manipulate. \ ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" will do so with the function \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Characters[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", and we can undo this operation by using \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["StringJoin[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", which puts them back together.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Characters[\"Characters\"]", "Input", AspectRatioFixed->True], Cell["StringJoin[%]", "Input", AspectRatioFixed->True], Cell[TextData[ "This function permits us to perform list operations on Strings without using \ the String functions and without breaking the Strings into Integer codes."], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Characters and Names"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["When ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " evaluates a string, it returns the string without quotation marks. When \ ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " evaluates a name which is undeclared, it returns the name. Hence it may \ be\ndifficult to distinguish between the result of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["i", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" the string and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["i", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[ "\" the variable! Be sure to\nenclose any character you intend to use with \ quotation marks, so that you don't run into trouble down the road. Here's an \ example of a problem of this sort:", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ list1 = Insert[{\"a\",\"b\",\"c\"},\"d\",4] list2 = Insert[{\"a\",\"b\",\"c\"},d,4] StringJoin[list1] StringJoin[list2]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Note that the peculiar symbol \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["<>", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is the operational equivalent for the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["StringJoin[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" command (much like \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["+", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is the operational equivalent of the \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Plus[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" command). In the last line, ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" is returning ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"abc\"<>d", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[", which is the equivalent of \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["StringJoin[\"abc\",d]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\", because, as usual, when ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" cannot perform an operation (in this case, joining the string ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"abc\"", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" to the variable ", Evaluatable->False, AspectRatioFixed->True], StyleBox["d", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["), it returns the command unevaluated.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[TextData["Converting Numeric Strings into Numbers"], "Section", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ "How could we convert the ", StyleBox["String", "Input", FontWeight->"Plain"], " \"", StyleBox["123", "Input", FontWeight->"Plain"], "\" into the ", StyleBox["Integer", "Input", FontWeight->"Plain"], " ", StyleBox["123", "Input", FontWeight->"Plain"], "? There are several methods we might try. We could first find the ASCII \ codes from the characters:" }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["ToCharacterCode[\"123\"]", "Input", AspectRatioFixed->True], Cell[TextData[ "Then we might realize that since the ASCII codes for \"0\" to \"9\" are 48 \ to 57, to convert from the codes to the actual numeric digits, we would \ simply need to subtract 48 from each. Subtracting a number from a list does \ this to each entry, so we could do the following:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["ToCharacterCode[\"123\"] - 48", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "Now we'd like to put these together into a number. But we don't want to \ add them; we first want to multiply each of them by the correct power of ten. \ The rightmost digit would be multiplied by 1, the next-to-last by 10, and so \ on. How can we create this list of powers of ten? Well, we\ncan use \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["Table[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["Table[10^j,{j,0,2}]", "Input", AspectRatioFixed->True], Cell[TextData[ "How did we know to stop at 2? Well, that was the length of our list, minus \ one. Now that we have the list of powers of ten, what can we do with it? We \ would like to multiply corresponding elements of our list of digits with our \ list of powers of ten, except that the powers of ten should be in reverse \ order. How's this:"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ ( ToCharacterCode[\"123\"] - 48 ) * Table[10^j,{j,2,0,-1}]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[ "Aha! Now we simply want to add these together. We're almost ready to \ define a conversion function from numeric Strings to Integers. (We haven't \ dealt with decimals or fractions and so on.) Here's how to convert \ \"123\":"], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ Apply[Plus,( ToCharacterCode[\"123\"] - 48 ) * Table[10^j,{j,2,0,-1}]]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[ "To make this a good function, we should make sure the 2 depends on the \ length of the String."], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\<\ string2Num[s_] := Apply[Plus,( ToCharacterCode[s] - 48 ) * Table[10^j,{j,StringLength[s]-1,0,-1}]]\ \>", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["Since ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " does not return strings with the enclosing quotation marks, it may not \ appear that \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["string2Num[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "\" performs any significant operation. To see that the function does \ convert numeric strings to numbers, we compare attempting to add \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["234", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox["\" to ", Evaluatable->False, AspectRatioFixed->True], StyleBox["1", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[" and \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["string2Num[\"234\"]", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox["\" to ", Evaluatable->False, AspectRatioFixed->True], StyleBox["1", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[":", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell["\"234\" + 1", "Input", AspectRatioFixed->True], Cell["string2Num[\"234\"] + 1", "Input", AspectRatioFixed->True], Cell[TextData[{ StyleBox["It is important to note that the parameter, ", Evaluatable->False, AspectRatioFixed->True], StyleBox["s", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[", does ", Evaluatable->False, AspectRatioFixed->True], StyleBox["not", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[ " have quotation marks placed around it at any point in the function \ definition. If \"", Evaluatable->False, AspectRatioFixed->True], StyleBox["string2Num[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["\" is called with ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"Cats\"", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" as the actual parameter, ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" will assign ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"Cats\"", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" to ", Evaluatable->False, AspectRatioFixed->True], StyleBox["s", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" when the function definition is executed, so ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"Cats\"", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox["---including the quotation marks---will be substituted for ", Evaluatable->False, AspectRatioFixed->True], StyleBox["s", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" at every point in the function definition. ", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Open ]] }, FrontEndVersion->"4.0 for Macintosh", ScreenRectangle->{{0, 1152}, {0, 850}}, WindowToolbars->{}, WindowSize->{520, 509}, WindowMargins->{{Automatic, 140}, {Automatic, 16}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}}, ShowCellLabel->True, ShowCellTags->False, RenderingOptions->{"ObjectDithering"->True, "RasterDithering"->False}, MacintoshSystemPageSetup->"\<\ 00<0004/0B`000003809T?o>old" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 103, 2, 98, "Title", Evaluatable->False], Cell[1845, 55, 99, 2, 26, "SmallText", Evaluatable->False], Cell[1947, 59, 1330, 47, 80, "Text", Evaluatable->False], Cell[3280, 108, 730, 19, 264, "SmallText", Evaluatable->False], Cell[CellGroupData[{ Cell[4035, 131, 95, 2, 50, "Section", Evaluatable->False], Cell[CellGroupData[{ Cell[4155, 137, 100, 2, 46, "Subsection", Evaluatable->False], Cell[4258, 141, 673, 11, 126, "Text", Evaluatable->False], Cell[4934, 154, 1325, 47, 114, "Text", Evaluatable->False], Cell[6262, 203, 63, 1, 27, "Input"], Cell[6328, 206, 51, 1, 27, "Input"], Cell[6382, 209, 59, 1, 27, "Input"], Cell[6444, 212, 1298, 48, 48, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[7779, 265, 91, 2, 46, "Subsection", Evaluatable->False], Cell[7873, 269, 1217, 39, 98, "Text", Evaluatable->False], Cell[9093, 310, 62, 1, 27, "Input"], Cell[9158, 313, 1382, 46, 78, "Text", Evaluatable->False], Cell[10543, 361, 656, 22, 62, "Text", Evaluatable->False], Cell[11202, 385, 64, 1, 27, "Input"], Cell[11269, 388, 75, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[11381, 394, 98, 2, 46, "Subsection", Evaluatable->False], Cell[11482, 398, 843, 20, 112, "Text", Evaluatable->False], Cell[12328, 420, 1081, 38, 94, "Text", Evaluatable->False], Cell[13412, 460, 115, 2, 30, "Text", Evaluatable->False], Cell[13530, 464, 125, 4, 42, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[13692, 473, 96, 2, 46, "Subsection", Evaluatable->False], Cell[13791, 477, 200, 4, 46, "Text", Evaluatable->False], Cell[13994, 483, 318, 12, 94, "Text", Evaluatable->False], Cell[14315, 497, 270, 5, 62, "Text", Evaluatable->False], Cell[14588, 504, 79, 1, 27, "Input"], Cell[14670, 507, 70, 1, 27, "Input"], Cell[14743, 510, 69, 1, 27, "Input"], Cell[14815, 513, 69, 1, 27, "Input"], Cell[14887, 516, 381, 13, 30, "Text", Evaluatable->False], Cell[15271, 531, 67, 1, 27, "Input"], Cell[15341, 534, 2005, 63, 146, "Text", Evaluatable->False], Cell[17349, 599, 70, 1, 27, "Input"], Cell[17422, 602, 68, 1, 27, "Input"], Cell[17493, 605, 934, 31, 78, "Text", Evaluatable->False], Cell[18430, 638, 1111, 36, 78, "Text", Evaluatable->False], Cell[19544, 676, 57, 1, 27, "Input"], Cell[19604, 679, 57, 1, 27, "Input"], Cell[19664, 682, 64, 1, 27, "Input"], Cell[19731, 685, 62, 1, 27, "Input"], Cell[19796, 688, 675, 23, 62, "Text", Evaluatable->False], Cell[20474, 713, 74, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[20585, 719, 97, 2, 46, "Subsection", Evaluatable->False], Cell[20685, 723, 1405, 46, 96, "Text", Evaluatable->False], Cell[22093, 771, 69, 1, 27, "Input"], Cell[22165, 774, 56, 1, 27, "Input"], Cell[22224, 777, 230, 5, 46, "Text", Evaluatable->False] }, Closed]], Cell[CellGroupData[{ Cell[22491, 787, 100, 2, 46, "Subsection", Evaluatable->False], Cell[22594, 791, 1251, 41, 98, "Text", Evaluatable->False], Cell[23848, 834, 170, 6, 72, "Input"], Cell[24021, 842, 2214, 76, 98, "Text", Evaluatable->False] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[26284, 924, 116, 2, 30, "Section", Evaluatable->False], Cell[26403, 928, 466, 17, 46, "Text", Evaluatable->False], Cell[26872, 947, 67, 1, 27, "Input"], Cell[26942, 950, 354, 6, 62, "Text", Evaluatable->False], Cell[27299, 958, 72, 1, 27, "Input"], Cell[27374, 961, 625, 17, 78, "Text", Evaluatable->False], Cell[28002, 980, 62, 1, 27, "Input"], Cell[28067, 983, 407, 7, 78, "Text", Evaluatable->False], Cell[28477, 992, 109, 4, 42, "Input"], Cell[28589, 998, 304, 6, 62, "Text", Evaluatable->False], Cell[28896, 1006, 121, 4, 42, "Input"], Cell[29020, 1012, 169, 4, 30, "Text", Evaluatable->False], Cell[29192, 1018, 156, 5, 57, "Input"], Cell[29351, 1025, 1452, 52, 80, "Text", Evaluatable->False], Cell[30806, 1079, 54, 1, 27, "Input"], Cell[30863, 1082, 66, 1, 27, "Input"], Cell[30932, 1085, 2011, 71, 98, "Text", Evaluatable->False] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)