Tag Info

Hot answers tagged

25

Symbols are created in the current context during parsing. This should not be a problem in normal circumstances as the symbols are merely "initialized" without values or properties. See these posts for more information: Is it possible to use Begin and End inside a Manipulate? Why doesn't this use of Begin[] work? When does Mathematica create a new ...


12

The main points of this answer are that,first, it seems rather difficult to have a fully universal mechanism for option-validation, and second, such a mechanism is not currently available in Mathematica on the language level (meaning automation of complete option-checking, including both the option's name and value). In the particular case in question, ...


9

Here is how I would approach this problem. First, when possible we should make use of the Attributes of the functions in use with regard to their effect on pattern matching. These attributes follow the natural application of the functions to which they apply so often they make mathematical matching easier. (Sometimes you don't want that.) The OneIdentity ...


8

No, AFAIK there is no way to see the FullForm and I think your conclusion is correct. The ^^ is not an operator, it is a form how you can input a number. Effectively, this behavior applies to all form of numerical input. For instance this is unholdable too HoldComplete[16*^2] (* HoldComplete[1600] *) Advanced expanation To give a more thorough ...


8

The simple answer is, if you want a string converted to StandardForm, you could wrap BoxData around it. E.g., CellPrint[Cell[BoxData["myFunction::usage=\"myFunction does ...\";"], "Input"]] But, in general, I wouldn't structure this as a question of CellPrint vs. FrontEnd`CellPrint. FrontEnd`CellPrint is undocumented, and therefore there is no contract ...


7

If you are familiar with AppleScript, you could try an approach like this: (* from http://github.com/fmeinberg/AppleScript *) AppleScript["RunFile", file_] := Run["osascript " <> file] AppleScript["RunScript", script_] := Block[{file = ToFileName[$TemporaryDirectory, "script.txt"]}, Export[file, script, "String"]; AppleScript["RunFile", file]] ...


7

Here is a parser that seems to satisfy your specs (with an improvement from Mr.Wizard): ClearAll[parse, sameQ, reduce]; parse[HoldPattern[Plus[terms__?parse]]] /; sameQ[terms] := True; parse[(a_: 1) * (_Sin | _Cos)] := True; parse[x_] := False; sameQ[terms__] /; MemberQ[{terms}, _Times] := sameQ @@ reduce[{terms}]; sameQ[__Sin | __Cos] := True; sameQ[__] ...


7

tokenize[str_] := Module[{exp, nb = CreateDocument[{ExpressionCell@ InputForm@MakeExpression[str, StandardForm]}, Visible -> False]}, SelectionMove[nb, Next, Cell]; exp = Flatten[ NotebookRead[nb][[1, 1]] /. {RowBox -> List, i_String /; StringMatchQ[i, Whitespace ..] :> Sequence[]}]; NotebookClose[nb]; exp[[3 ...


7

This seems to work: StringCases["blabla ...Hello Hello ... blabla ... Goobye Goobye ..", Longest[___ ~~ a : "Hello"] ~~ b : Shortest[___ ~~ "Goobye"] :> a ~~ b] Update If there are multiple substrings to extract you can use recursion: extractbetween[str_, x_, y_] := Module[{f}, f[s_] := StringCases[s, Longest[a___ ~~ x] ~~ b : Shortest[___ ~~ ...


6

I think this particular scenario has to do with how you can create your own Import/Export filters: Developing an Import Converter Regarding 'verification' as in the Plot[Sin[x], {x, -Pi, Pi}, Frame -> True, FrameTicksStyle -> Red] example given by Nasser, keep in mind that you might have options parameterized like so: Manipulate[ Plot[Sin[x], {x, ...


6

Using the internal expression parser: string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; Cases[ ToHeldExpression[string], s_Symbol * Except[_Symbol, n_?NumericQ] :> {HoldForm[s], n}, {-2} ] {{x1,0.97}, {x2,0.521}, {x3,-0.07431}, {x4,-0.126}, {x5,-0.1939}, {x6,-0.361}} Note use of HoldForm to keep ...


5

I'm posting this variant in the hopes that it will be a little more educational. Otherwise doesn't add anything over Kuba's version. Generally, parsing can be done using StringCases. You'll need to build up a string expression that describes the pattern of the file name, much the same way you'd write "%d_%s_Polarizer%dDeg-Temp%dK.dpt" when working with ...


5

list = {"20140605_SampleName-C-vert_Polarizer0Deg.dpt", "20140605_SampleName-C-vert_Polarizer90Deg-Temp100K.dpt", "20140606_SampleName-C-vert_Polarizer0Deg-Temp10K.dpt"} There are many ways. This one is not bulletproof but I think it should work with your data: parse[string_] := ToExpression[{DateList@#, ##2}] & @@ Flatten[ StringCases[string, ...


5

shortestStringCases[str_String, from_String, to_String] := StringCases[ str, (from ~~ mid___ ~~ to) /; StringFreeQ[mid, {from, to}]] shortestStringCases["blah X blah X first Y blah X blah X second Y", "X", "Y"] (* {"X first Y", "X second Y"} *)


4

The benefit of using a symbolic tree representation with inert heads as in Leonid's parser is that you can then decide how to represent the data. And that is indeed what you should do, instead of extracting the elements using Cases. Here's an example using your parsed output above: Block[{ulContainer, liContainer}, ulContainer[_, l__] := {l}; ...


4

Figured out what I'm going to do: Read a line from the file. Try converting it to an expression (ToExpression[]). If successful, I've found an expression (or a comment or a blank line). If not, grab additional lines and concatenate them with the line that failed to parse until I find a series of lines that succeeds. The code: ClearAll[breakExpressions]; ...


4

This answer is just for exercise purposes, I would use Coefficient way. string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; StringCases[ StringReplace[string, "- " -> "-"], c : NumberString ~~ _ ~~ x : ("x" ~~ DigitCharacter) :> {c, x}] {{"0.97", "x1"}, {"0.521", "x2"}, {"-0.07431", "x3"}, {"-0.126", ...


3

There are several ways it might be done. This is one example. Here is something like your string: str = "y=0.97*x1+0.52*x2-30.21-0.07*x3"; I do not take the full version, since you did not give it in M form, which normally you should have done. Now, let us make a list out of it: list = List @@ ToExpression[str] (* {-30.21, 0.97 x1, 0.52 x2, -0.07 x3} ...


3

If you're OK with using emacs, there's a mode which allows it to act as a front-end. There are also modes for editing m-files, eg this.


3

A possible solution is just to replace your boundary words with single characters. I think what you are venturing into is something akin to look-behind, which I don't think is supported. Anyways here's how I would do it: boundary = {"Hello", "Goobye"}; limits = {"\[FormalCapitalX]", "\[FormalCapitalY]"}; shift[str_, from_, to_] := StringReplace[str, Rule ...


3

This is not a complete answer but has too much for a comment. I don't have Numbers so do not know what is in the file you linked but the following information can be obtained: Import["PathTo/example.numbers", "ZIP"] (* {"QuickLook/Thumbnail.jpg", "buildVersionHistory.plist", "index.xml"} *) Now grab the XML: Import["PathTo/example.numbers", ...


2

MyLogicalExpand[expr_] := With[{patt = "(" ~~ x : (Except[Characters["()"]] ..) ~~ ")" /; ( Implies[#, ! #2] & @@ ( MemberQ[StringPosition[x, LetterCharacter][[;; , 1]], #] & /@ {2, 1})) }, Module[{ cas = StringCases[expr, patt], pos = StringPosition[expr, patt], ...


2

If you have a more powerful box available, you could install Mathematica there, and run it from the netbook using X forwarding over SSH (ssh -X). The UI should be responsive as long as you have a good network connection and you aren't displaying large graphics or plots. Some documentation for setting up an SSH server: ...


2

Using jVincent's ContextScope[] m`str1 = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; ContextScope["m`", {SymbolName /@ Variables@#, Coefficient[#, Variables@#]} &@ToExpression[str1] // Transpose] (* {{"x1", 0.97`}, {"x2", 0.521`}, {"x3", -0.07431`}, {"x4", -0.126`}, {"x5", -0.1939`}, {"x6", ...


1

list = {"20140605_SampleName-C-vert_Polarizer0Deg.dpt", "20140605_SampleName-C-vert_Polarizer90Deg-Temp100K.dpt", "20140606_SampleName-C-vert_Polarizer0Deg-Temp10K.dpt"}; Another way using RegularExpressions: parse[str_]:=Module[{time,deg,temp}, ...


1

Longtime user, first time responder. I am going to assume that by parsing you mean extracting values from JSON data and not transforming data into JSON. There are a couple ways to parse JSON in Mathematica. Assuming the data is already a set of rules in Mathematica using Import[data, "JSON"] or something like that. data = {blah -> blah, field1 ...


1

As already stated in the comments the ^^ notation is handled in parsing; observe: HoldComplete[16^^ff] // FullForm HoldComplete[255] (I intend this to illustrate that this notation is "evaluated" before the main evaluator ever sees it.) This parsing is really no different from other numerical notation in Mathematica, for example 12.345 is directly ...


1

Since I'm still new to String Patterns here is another entry using StringCases. string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; StringCases[string, s : ({" ", " + ", " - "} ~~ NumberString) ~~ "*" ~~ x : ("x" ~~ DigitCharacter ..) :> {x, ToExpression@s}] Gives: {{"x1", 0.97}, {"x2", 0.521}, {"x3", ...


1

I see the answer completely in the comments. Use data = Transpose@Rest@Import[ "https://raw.githubusercontent.com/camenergydatalab/EnergyDataSimulationChallenge/master/challenge1/data/dataset_500.csv"]; You can then verify that this was your intention with TableForm@data Hit the "Show Full Output" button, and you will get a very long table that is ...


1

This, with a suitable transform function to traverse the tree, would be an adequate tokenizer: TreeForm[Hold[ Plot3D[{x^2 + y^2, -x^2 - y^2}, {x, -2, 2}, {y, -2, 2}, RegionFunction -> Function[{x, y, z}, x^2 + y^2 <= 4]]]]



Only top voted, non community-wiki answers of a minimum length are eligible