Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to convert the following list of strings to computable numbers with the comments preserved?

 str = {{{"8.0,1034(*g opx ksp pl ilm liq q *)"}, {"8.0,1035(*g opx ksp pl ilm liq q *)"}, 
         {"8.0,1037(*g opx ksp pl ilm liq q *)"}, {"8.0,1038(*g opx ksp pl ilm liq q *)"},   
         {"8.0,1039(*g opx ksp pl ilm liq q *)"}, {"8.0,1040(*g opx ksp pl ilm liq q *)"}},  
        {{"8.0,886(*g opx ksp pl ilm liq q  *)"}, {"8.0,889 (*g opx ksp pl ilm liq q *)"},   
         {"8.0,891(*g opx ksp pl ilm liq q  *)"}, {"8.0,894 (*g opx ksp pl ilm liq q  *)"},   
         {"8.0,896(*g opx ksp pl ilm liq q  *)"}, {"8.0,899 (*g opx ksp pl ilm liq q  *)"}}}

I'm after something like:

strc = {{{8.0,1034(*g opx ksp pl ilm liq q *)},....

Perhaps there is a way involving programatically adding the comments back onto the numbers after the numbers have been converted to an expression? Any Suggestions?

share|improve this question
3  
To do this robustly (expressions of any type) I believe you will need to parse the strings, and that was done very nicely in answer to this question: mathematica.stackexchange.com/q/17648/121 –  Mr.Wizard May 31 '13 at 1:22
    
@Mr.Wizard I really like your link (+1), but I disagree that you need to parse strings. To get the string in the first place is hard to do programmatically (and is not addressed by Leonid). In case quotes are present in the expression, we cannot simply put ""'s around it. In case comments are present, we cannot resort to ToString, as it would delete the comments and destroy formatting. I have looked at front end stuff like CopyToClipBoard a while ago but I think I was unable to make a function that would "put quotes around an expression in a smart way", maybe I should ask a question about it. –  Jacob Akkerboom May 31 '13 at 12:25
    
However, getting the cell expression programmatically is easy, as shown in my answer below. Also see my answer here, in your linked Q&A ,to see how we can also use this to delete comments and keep our formatting. (also @Kuba) –  Jacob Akkerboom May 31 '13 at 12:27
    
@Mr.Wizard you can also get the string from the cell expression using ToString[NotebookRead[Cells[CellTags -> "targetCell"][[1]]][[1, 1]], InputForm], but I guess that should feel indirect. –  Jacob Akkerboom May 31 '13 at 12:51
    
@Jacob Good points, and +1 on your answer. –  Mr.Wizard Jun 2 '13 at 12:15
add comment

2 Answers

You can start with that.

StringCases["8.0,1034(*g opx ksp pl ilm liq q *)", 
 x__ ~~ y : ("(*" ~~ ___ ~~ "*)") :> {x, y}]

{{"8.0,1034", "(*g opx ksp pl ilm liq q *)"}}

And You can do with the first elements what You need.

Edit

I can not be passive after Jacob's remarks :) Following solution is based on his but simplified in order to avoid complicated procedures. I have also taken into account his comments to this post.

CellPrint[Cell[#, "Input"]] &@
 BoxData[ToBoxes[str] /. 
   x_String?(StringMatchQ[#, 
        "\"*" ~~ Verbatim["(*"] ~~ ___ ~~ Verbatim["*)\""]] &) :> 
    StringTake[x, {2, -2}]]

Definitely it works and is simple as it can be. I'm not sure why CellPrint[Cell[#, "Input"]] &@ BoxData[RowBox[{"{expr(*comm*)}"}]] gives us what we need. I hope Jacob Akkerboom will tell us more.

share|improve this answer
2  
+1 y : ("(*" ~~ ___ ~~ "*)") is nice. –  David Carraher May 30 '13 at 13:40
2  
Pff Kuba, I helped you twice, and now you are stealing my points with impressive but weak magic :P. Note that StringMatchQ["()", ("(*" ~~ ___ ~~ "*)")]-> True ;). You might as well have left out the asterix (*), it has special meaning in string pattern matching. I'm kidding, I shouldn't care about points, but please also see my answer :). (@DavidCarraher) –  Jacob Akkerboom May 30 '13 at 20:39
1  
Cool, looking forward to that :). Anyway the pattern you are really after is "(" ~~ _?(MatchQ[#, "*"] &) ~~ ___ ~~ _?(MatchQ[#, "*"] &) ~~ ")". –  Jacob Akkerboom May 30 '13 at 20:53
1  
The above is nicer with Verbatim I guess, but I can't really show it as this will just format my code instead of display what I want. So we want something like "(" ~~ Verbatim["*"] ~~ ___ ~~... –  Jacob Akkerboom May 30 '13 at 21:19
1  
Wow Kuba, now this good stuff, +1 :). Much more practical than my answer, using the existing RowBox works fine, you make good use of its flexibility. Your (replacement) rule can be written a bit shorter, I'll suggest an edit, but its up to you what you like best :). –  Jacob Akkerboom May 31 '13 at 10:06
show 3 more comments

About the first section: This simulates the behavior of Mathematica a bit more closely than Kuba's answer, creating an additional RowBox. However, the choice of how general the different aspects of the solution are may be a bit odd. Kuba's answer feels cleaner.

Use the OPs definition of str. The following will generate an input cell where the comments are (still) present as comments.

store[str_String] := (Sow[str]; token);

CellPrint[Cell[#, "Input"]] &@
 BoxData[
  ReplacePart[#[[1]],
     MapThread[
      Rule, {Position[#[[1]], "token"], 
       Composition[RowBox, List] /@ #[[2, 1]]}]] &@
   Reap[ToBoxes@Map[Composition[store, ToString], str, {2, 2}]]]

Format preserving alternative

Suppose we start with the input cell

str = 
{{{"8.0,1034(*g opx ksp pl ilm liq q *)"}, {"8.0,1035(*g opx \
ksp pl ilm liq q *)"}}, {{"8.0,886(*g opx ksp pl ilm liq q  *)"}}}

We can give this a celltag by putting our cursor in the cell and doing Cell>Show Expression from the menu. The resulting cell will be the same as the cell printed by

Cell[BoxData[
    RowBox[{"str", "=", 
       RowBox[{"{", 
          RowBox[{
             RowBox[{"{", 
                RowBox[{

             RowBox[{"{", 
               "\"\<8.0,1034(*g opx ksp pl ilm liq q *)\>\"", "}"}], 
             ",", 

             RowBox[{"{", 
               "\"\<8.0,1035(*g opx ksp pl ilm liq q *)\>\"", 
               "}"}]}], "}"}], ",", 
             RowBox[{"{", 

           RowBox[{"{", 
             "\"\<8.0,886(*g opx ksp pl ilm liq q  *)\>\"", "}"}], 
           "}"}]}], 
          "}"}]}]], "Input", CellTags -> {"targetCell"}] // CellPrint

We can now do

NotebookRead[Cells[CellTags -> "targetCell"]] /. 
  x_String?(StringMatchQ[#, 
       "\"*" ~~ Verbatim["(*"] ~~ ___ ~~ Verbatim["*)\""]] &) :> 
   StringTake[x, {2, -2}] // CellPrint

to get a cell that is formatted in the same way as the cell with CellTag "targetCell", except for any formatting that may occur inside strings.

Format ignoring option using strings

Mr.Wizard hinted at a solution using strings, and Rm-rf gave the main code for this

CellPrint[Cell[#, "Input"]] &@
 First@MathLink`CallFrontEnd@
   UndocumentedTestFEParserPacket[ToString[str], False]

Which seems nice for this question, especially as having comments in strings hints at the expression str being the result of some Import command by Mathematica, or that this expression is at least generated in some way. In this case you don't care about existing formatting and in case of Import maybe it is an option to do all parsing by UndocumentedTestFEParserPacket. Who knows

share|improve this answer
    
@geordie woops, I have added that. –  Jacob Akkerboom May 30 '13 at 13:26
    
Very nice. It works perfectly. I think I'll have to do some reading about cell expressions.. Many Thanks! –  geordie May 30 '13 at 13:36
    
@geordie yay :), then I am glad. I have done this kind of thing before without any boxes (and comments for that matter), but that was not so nice. This was good practise for me :). –  Jacob Akkerboom May 30 '13 at 13:40
1  
@geordie Ah yes, nice. I upvoted that question (which I don't do often) and favorited it. I wanted to present a solution yesterday but I had no time. I also saw your solution, but I don't like working with strings, I prefer held expressions. I always regret decisions like determining the scope of a variable. So I'm very interested in how we can do something like that afterwards :). –  Jacob Akkerboom May 30 '13 at 13:55
1  
You can save yourself the trouble of constructing the boxes by using UndocumentedTestFEParserPacket. For example, First@MathLink`CallFrontEnd@UndocumentedTestFEParserPacket[s, False] where s is a string –  rm -rf May 31 '13 at 13:12
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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