3

I am trying to write a program which will generate a list of objects and then exports it in a separate file and clears its memory. As an example I tried the following

Do[{Subscript[l, p] = Table[Subscript[l, p, i], {i, 10}];
  Export["\!\(\*SubscriptBox[\"l\", \"p\"]\).mnaz", 
   Compress[Subscript[l, p]], "String"]; Clear[Subscript[l, p]]}, {p, 
  1, 3}]

Of course, the above is just a sample (and my actual list(s) are more bulky (actually they are list of matrices)), but I hope it explains the spirit. I wanted to export each list separately so that I can get separate files like l_1.mnaz, l_2.mnaz etc., which I can import again by using the same type of code.

Motivation: I need to actually generate a huge list of data. However whatever I do, my system goes out of memory after 5000 loops, which itself takes a lot of time. One reason is that, I need to modify the list at each loop. It seems 1000 loops takes a reasonable time. Hence I am trying to break the loop into smaller chunks, export the data, and clear the memory for the next loop. Clear[Subscript[l, p]] is used for that purpose.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
RSG
  • 601
  • 4
  • 13
  • Subscripts don't work in any filename system I know of, so I think that will cause problems. – Verbeia Jul 29 '12 at 09:11
  • @Verbeia even if they did "\!\(\*SubscriptBox[\"l\", \"p\"]\).mnaz" would not work. One could instead use: ToString[Subscript[l, p], TeXForm] <> ".mnaz" or if the file system supports it something besides TeXForm. – Mr.Wizard Jul 29 '12 at 09:25

1 Answers1

8

Try this:

Do[
 l[p] = Table[Subscript[l, p, i], {i, 10}];
 Export["l_" <> ToString[p] <> ".mnaz", Compress@ l[p], "String"];
 l[p] =.
 , {p, 1, 3}
]

I replaced Subscript[l, p] with l[p]. Frankly I don't see a reason to not simply use temp = Table[ . . . but I am trying to keep to the spirit of your example.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371