Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

My ultimate goal is to have a text file that contains the following information in the following format exported to a text file:




I will essentially have three lists that I start with (otherdata, list1x, and list2). I need to increment each element in the list by a known amount a set number of times.

In this particular case, list1x has five elements:

list1x = {1,1.1,1.2,1.3,1.4};

Now I want to build a second list that increments each element by, let's say, 0.5 and then have it make a new list:

list2x = {1.5,1.6,1.7,1.8,1.9};

And so on and so forth a set number of times that I choose (like 1000). After I build the lists I then want to export a text file in the format that I have shown above.

I'm pretty new to Mathematica and was wondering if there was a simple, elegant way to do this without a million For or While loops. Using something like Sow and Reap perhaps?

Anything would be useful at this point! Thanks so much in advance!

share|improve this question

closed as too localized by Yves Klett, Artes, Sjoerd C. de Vries, Jens, m_goldberg Apr 29 '13 at 0:25

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

you can create a list of the list1x, list2x etc lie this: NestList[ # + .5 &, {1, 1.1, 1.2, 1.3, 1.4}, 5 ]. You didn't say how list1 etc are to be created. You can output all these with Export (which is described in the docs and lots of places here) –  acl Mar 28 '13 at 16:15
The NestList works fairly well. It's more the getting the data to be in the format I showed in my question. Exporting a list of data is easy and straightforward. It's exporting the data in the format that is messing me up. –  Timothy Mar 28 '13 at 16:25
I guess my point is, it's not clear what the problem is. You have a bunch of lists, just make them strings, join them, with line breaks at the right place and export them. Is that what you don't know? –  acl Mar 28 '13 at 17:08

1 Answer 1

Data building:

data = With[{
   otherdata = {11, 44, 33},
   list2 = {5, 8, 13, -17, 10},
   list1x = {1, 1.1, 1.2, 1.3, 1.4}},
  Table[{"w1" <> ToString[i + 1],
    otherdata, list1x + .5 (i - 1), list2}, {i, 3}]];


Export[NotebookDirectory[] <> "output.txt",
 ExportString[Flatten[Riffle[data, ""], 1], "Lines"]]

Into the directory where you saved the notebook. I appended zero string to the end of each Table sublist (with Riffle) so that ExportString spaces paragraphs with an empty line.

share|improve this answer
Thanks for the answer BoLe. That was almost exactly what I was looking for. I was able to tweak the snippet of code you provided and get the result I desired. Once again, thanks a bunch! –  Timothy Apr 2 '13 at 12:07

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