0
$\begingroup$

There's a list such as {0.1,2,0.15,5}. I want to know how to convert the decimal to integer( but begin with zero 0), and don't touch the integer. In the second step, joint the data to a string "a01b2c015d5". Note that the data in the printed string are separated with a,b,c and d, because I have tons of data to mark with a, b, c and d. Finally, export a01b2c015d5={0.1,2,0.15,5} to the file exportData.txt. How to do it? Thank you!

$\endgroup$
2
  • 1
    $\begingroup$ This will generate the string you want: StringReplace[ToString@Thread[{{a, b, c, d}, {0.1, 2, 0.15, 5}}], {"," -> "","." -> "", " " -> "", "{" -> "", "}" -> ""}]. But I assume you'd want to do this repeatedly, for your entire dataset. To tell you how to do that, we'd need to see a minimal working example of your dataset, so we can see the structure. $\endgroup$
    – theorist
    May 10, 2021 at 4:42
  • $\begingroup$ @theorist, Thanks, this is a great idea. $\endgroup$
    – likehust
    May 10, 2021 at 6:12

1 Answer 1

1
$\begingroup$

Try

StringJoin[StringTake[StringReplace[ToString[Flatten[Transpose[
  {{a,b,c,d},{0.1,2,0.15,5}}]]],{","->"","."->""," "->""}],{2,-2}],
  "=",ToString[{0.1,2,0.15,5}]]

Start from the innermost nested function, strip that out, run it, see what it does, then look it up in the documentation and see if you can understand why that does what it does and how it gets the input slightly closer to the output that you want, then repeat with each next outer layer.

Then you can export that resulting string to your file.

If you carefully do that then you should greatly improve your skills to be better able to do your next problem.

$\endgroup$
4
  • $\begingroup$ Thanks. It's really a short and effective way to the point. $\endgroup$
    – likehust
    May 10, 2021 at 6:06
  • $\begingroup$ Thank you @Bill, I want to know why the code from exported file can not be used to calculate when the exported file format is .m, for example the file name is exported.m, the form of exported data is "a01b2c015d5={0.1, 2, 0.15, 5}", which includes a pair of double quotation marks. How to export the file for using the exported codes directly? Thanks $\endgroup$
    – likehust
    May 10, 2021 at 8:40
  • $\begingroup$ Thank you very much. $\endgroup$
    – likehust
    May 10, 2021 at 8:51
  • 1
    $\begingroup$ Read en.wikipedia.org/wiki/XY_problem Maybe you could edit your original question to say what you REALLY want to accomplish, instead of asking how to do one step that you think will get you where you want to go. Perhaps with the real problem clearly described then someone might be able to suggest a simpler way to get where you need to go. $\endgroup$
    – Bill
    May 10, 2021 at 9:05

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.