Tag Info

Hot answers tagged

26

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, ...


11

I worked on Interpreter. As far as the implentation is now, the DelimitedSequence parser does not support quoting, so what you want can't be done. We'll try to add it in a future version.


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

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 ...


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

This is an ideal use case for SemanticImport, but unfortunately it has issues getting the commas right in version 10.0. Luckily, version 10.0.1 has already fixed this bug:


7

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 ...


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

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

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[___ ~~ ...


7

Just define a new function that could take different inputs, either through overloading or using If, Which, or Switch. Clear[f] f[x_. Power[z, e_.]] := {x, e} f[Times[x_, Power[z, e_.]]] := {x, e} f[x_] := {x, 0} Cases[lis, y : Alternatives[x_., x_. Power[z, e_.], Times[x_, Power[z, e_.]]] :> f[y]] (* {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} *) ...


7

For the first case, one way will be to read the entire file as a String and manipulate it e.g: txt = ReadList["dose.txt", String]; Then do something like: Flatten @ StringCases[txt, "Mean Dose [%]: " ~~ (x : NumberString) -> ToExpression @ x] {103.3} I guess you can turn it into a function: sValues[s_String] := Flatten @ StringCases[txt, s ~~ ...


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

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"} *)


5

One possibility is to use Replace with Sow/Reap: Scan[ Replace[#, { x_. Power[z, e_.] :> Sow[{x, e}], Times[x_, Power[z, e_.]] :> Sow[{x, e}], x_ :> Sow[{x, 0}]}] &, lis] // Reap // #[[-1, -1]] & {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} However, the wildcard x_ could be tricky if you needed to operate deeper ...


5

Another possibility is to use ReplaceAll on the rhs of RuleDelayed in the second argument of Cases: rules = Alternatives[x_. Power[z, e_.] :> {x, e}, Times[x_, Power[z, e_.]] :> {x, e}, x_. :> {x, 0}]; (* your prefered second argument for Cases *) Cases[lis, pat : rules[[All, 1]] :> (pat /. List @@ rules)] (* {{6, 0}, {1, ...


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", ...


4

The General Case Alas, Cases does not permit multiple replacement rules. But Replace does, although we must take care to 1) create an output list, 2) omit non-matching elements and 3) operate only upon the first level: lis = 6 + 1/z^4 + 4/z^2 + 4 z^2 + z^4; Replace[List @@ lis, {x_. z^e_. :> {x, e}, x_ :> {x, 0}, _ :> Sequence[]}, {1}] (* ...


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", ...


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 ...


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: ...



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