1
$\begingroup$

I'd like to export string, for example, like this:

"ZT[1,1]={"blabla", 0, n+13, 4*g1*g2/3 + g2^2};"

to myfile.m and there i want to see:

ZT[1,1]={"blabla", 0, n+13, 4*g1*g2/3 + g2^2};

but no matter what I do, I get other results, for example, like this:

"ZT[1,1]={\"blabla\", 0, n+13 , 4*g1*g2/3 + \ g2^2}"

What I did:

PutAppend[StringJoin["ZT[",ToString[i],",",ToString[k],"]=" ,"{", ToString[Last[ReadList[StringJoin["!some command ","'",ToString[Z[i,k][[1]]],"'"],String]]] ,"}"], StringJoin["some_table_up_",ToString[somenumber],"_babam.m"]]

If I wrote ToExpression, I can get only:

{"blabla", 0, n+13 , 4*g1*g2/3 + g2^2}

but I need a variable before it.

Thank you so much!

$\endgroup$
2
  • 2
    $\begingroup$ What's wrong with WriteString? That is exactly what you want to do, I think. $\endgroup$
    – b3m2a1
    Jul 18, 2018 at 0:19
  • 1
    $\begingroup$ Nothing =), just among the abundance of mathematica commands, I did not reach it) Thank you! $\endgroup$ Jul 18, 2018 at 10:35

1 Answer 1

1
$\begingroup$

Use Unevaluated:

Put["first line", "tmp.m"]
PutAppend[
    Unevaluated[ZT[1,1]={"blabla",0,n+13,4*g1*g2/3+g2^2};],
    "tmp.m"
]
Import["tmp.m","String"]

"first line"

ZT[1, 1] = {"blabla", 0, n + 13, 4*g1*(g2/3) + g2^2};

If you must use a string instead of an expression, then you can do:

With[
    {
    str=ToExpression[
        "ZT[1,1]={\"blabla\", 0, n+13 , 4*g1*g2/3 + \ g2^2}",
        StandardForm,
        Unevaluated
    ]
    },

    Put[1,"tmp.m"];
    PutAppend[str, "tmp.m"]
]

Import["tmp.m", "String"]

1

ZT[1, 1] = {"blabla", 0, n + 13, 4*g1*(g2/3) + g2^2}

$\endgroup$
1
  • $\begingroup$ Thank you for your answer! It helped me as well as option of solution suggested by b3m2a1! $\endgroup$ Jul 18, 2018 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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