0
$\begingroup$

I'm fairly new to Mathematica and I'm trying to manipulate some data but its not going very well. I want to import a text file into Mathematica(its a list of chapter headings and page numbers of a book) and be able to print out the page number when i/user type(s) in the chapter heading. How do i go about it? Any help will be appreciated.

$\endgroup$
  • $\begingroup$ Please provide more details about your list. Preferably provide a small sample of, say, 10 lines. $\endgroup$ – Sjoerd C. de Vries Sep 19 '15 at 12:04
1
$\begingroup$

Let list be your list of chapter headings and page no

 list = {{"my first heading", 2}, {"My 2 heading",7}, {"Heading no. 3", 11}};

You can use the function given below which takes in the chapter heading as its argument and spits out the corresponding page number as listed in list

 f[chapterHeading_] := 
  DeleteCases[
   Flatten[Table[
    If[Sort[Characters[
    StringReplace[StringTrim[ToString[chapterHeading]], 
     Whitespace -> ""]]] === 
   Sort[Characters[
    StringReplace[StringTrim[list[[i, 1]]], Whitespace -> ""]]], 
  Sow[list[[i, 2]]]], {i, 1, Length[list]}]], Null]

Now we test the function by evaluating f[my first heading] which gives {2}. The chapter headings are case sensitive but you can type without spaces which means f[myfirstheading] also gives {2}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.