Tag Info

New answers tagged

5

ImportString is all you need, as demonstrated below:


6

The simplest way to express the transformation rules for StringReplace would be to write them explicitly (here using the traditional Caesar Cipher three-character shift): StringReplace["THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" , { "A"->"X","B"->"Y","C"->"Z","D"->"A","E"->"B" , "F"->"C","G"->"D","H"->"E","I"->"F","J"->"G" ...


6

This answer is just a complement to Öskå's, showing off the new operator forms introduced in v10. For compatibility with earlier versions, see his answer. az = CharacterRange["a", "z"]; Notice the use of ReplaceAll with a single argument to construct a replacing operator: encode = ReplaceAll@Dispatch@Thread[az -> RotateLeft[az, 13]]; ...


5

rule[n_] := With[{a = CharacterRange["a", "z"]}, Thread[a -> RotateLeft[a,n]]] res = StringJoin[StringSplit["hello", ""] /. rule[2]] "jgnnq" StringJoin[StringSplit[res, ""] /. Reverse /@ rule[2]] "hello" Or to follow the example in the Wikipedia page: StringJoin[StringSplit[CharacterRange["a", "z"], ""] /. rule[-3]] ...


0

I don't yet know where the rounding is coming from but it appears to be occurring during formatting. Consider: testset[fnStr2Real, {"a", "b"}] // Normal // Normal {{"a" -> 529251., "b" -> 520358.}, {"a" -> 51., "b" -> 0}, {"a" -> 177., "b" -> 0}, {"a" -> 125., "b" -> 0}, {"a" -> 1343., "b" -> 0}} Actually the rounding ...


3

I would use StringFreeQ: (* files = {"10.txt", "11.txt", . . ., "inelasticov3-8.txt", "inelasticov3-9.txt"}; *) Select[files, StringFreeQ[#, "inelastic"] &] {"10.txt", "11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", "18.txt", "19.txt", "1.txt", "20.txt", "2.txt", "3.txt", "4.txt", "5.txt", "6.txt", "7.txt", "8.txt", ...


2

To get the file list without the files containing "inelastic", you can use: Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Then, to import them: Import[#] & /@ % One-step solution: Import[#] & /@ Cases[{your file list here}, x_String /; StringMatchQ[x, "*inelastic*"] == False] Edit: Out of curiosity, ...


2

You need to use the pattern "*inelastic*.txt" in FileNames. This will only return the list of files you need. Then use Map with pure functions (Function) to import all of them in one go: Import[#, "Table"]& /@ fileNames or similar. To use everything except file names with "inelastic", you can use Select,Cases,DeleteCases, etc. withStringMatchQ`. ...


2

I know this is a simple question and there is a single command to do this but I have been looking for the command for 30+ minutes and cannot find it. Let's address that. In either version 7 or version 10 searching the documentation for: convert table to string brings up ImportString as one of the first results; 2nd in v10, and 5th in v7. In that ...


6

Using ExportString: ExportString[list, "TSV"] If you want to make sure it's right: ExportString[list, "TSV"] // InputForm


1

Try something like this: tb = Table["\t", {i, Length[list[[1]]]}]; Join[Transpose[{ToString /@ #1[[1]], #2}], Transpose[{ToString /@ #1[[2]], #2}]] &[list, tb] // StringJoin


3

I do want to support something like this with some custom syntax, though I can't guarantee when that will actually happen. In the meantime, you can do this: StringTemplate["Pi is <*NumberForm[N[#],3]*>!"][Pi] which produces: "Pi is 3.14!" Unfortunately TextString isn't that smart yet about NumberForm, and doesn't support EngineeringForm and ...


3

This is my first attempt: Clear[formatPattern, formatPatternQ, applyFormat, applyFormatToValue, \ builtinFormatFunction] formatPattern = StartOfString ~~ "%" ~~ format : (DigitCharacter ... ~~ ("." ~~ DigitCharacter ..) ...) ~~ formatName : WordCharacter ... ~~ rest : ___; formatPatternQ = StringMatchQ[#, formatPattern] &; applyFormat[t_] := ...


1

If you really want to use Position :)... Position[list, s_String?(! StringFreeQ[#, "a"] &)] {{1}, {2}, {3}}


1

list = {"a(0)", "a(1)", "a(2)", "b(42)"} You can get the elements that match like so: els = Select[list, StringMatchQ[#, "a" ~~ ___] &] (* {"a(0)", "a(1)", "a(2)"} *) If you want their positions, you can map Position over this list: Position[list, #] & /@ els (* {{{1}}, {{2}}, {{3}}} *)


3

list = {"a(0)", "a(1)", "a(2)"}; StringPosition[list, "a"] {{{1, 1}}, {{1, 1}}, {{1, 1}}}


1

Maybe p = {"abc", "cd"}; string = "abcdabcdcd"; StringCases @@@ Thread[{string, p}] (* {{"abc", "abc"}, {"cd", "cd", "cd"}} *) Original post: StringCases[string, p, Overlaps -> All] (* {"abc", "cd", "abc", "cd", "cd"} *)


8

(All observations made in version 7.) There seems to be a limitation for input even in the Front End (Notebook interface), in that if I enter more than 766 levels of nested lists I get a MaxFormatDepthExceeded expression and an error beep. The help text is: A box structure with a depth exceeding the maximum allowed depth was encountered. We can at ...


5

What you experience here seems to be some kind of stack limit when you have nested expressions. It doesn't seem to matter whether you nest lists or function calls. Look for instance at this example here which is nothing more than a nested call f[f[f[...f[a]]..] On the other hand, if the parser doesn't need to build up such a large stack, it seems to ...


3

In version 10 (available now through the programming cloud) you can use StringTemplate. StringTemplate allows using named placeholders, classical numbered placeholders, or even embedding code that uses placeholders. See the documentation for many advanced usage examples. Basic usage demonstrating both named and numbered templates: template = ...


3

f[{a_, b_, c_}] := "AttributeBegin Translate " <> #3 <> " Color " <> #2 <> " Surface \"constant\" Sphere 0.1 -0.1 0.1 360.0 Attribute \"light\" \"shadows\" \"on\" LightSource \"pointlight\" \"l" <> #1 <> "\" \"intensity\" 1 \"lightcolor\" [ " <> #2 ...


6

Maybe: lightTemplate = "AttributeBegin Translate `5` `6` `7` Color `2` `3` `4` Surface \"constant\" Sphere 0.1 -0.1 0.1 360.0 Attribute \"light\" \"shadows\" \"on\" LightSource \"pointlight\" \"l$num$\" \"intensity\" 1 \"lightcolor\" [ `2` `3` `4` ] AttributeEnd Illuminate \"`1`\" "; conv = ...


0

One liner: Table[expr,Evaluate[Sequence@@({#,lim}&/@Array[Subscript[p, #] &, n])]]


0

p0 = Array[Subscript[p, #] &, 7] l0 = Table[1 + Mod[i, 3], {i, 7}] Table[q, Evaluate[Sequence @@ Transpose[{p0, l0}]]]



Top 50 recent answers are included