3
Do[
  PutAppend[Table[i, {RandomInteger[100]}], "./file.m"],
  {i, 100}
]

I'd like each inner list to always appear on the same line, but not automatically wrapped at 68th character. How to achieve that? I wonder where and how the PageWidth option should be set for the InputStream used by the PutAppend.

#!/usr/bin/env bash

sed -e '
    /.* $/ {
        N
        /\n .*$/ {
            s/\(.*\) \n \(.*\)/\1 \2/
        }
    }
' $1

will fix the broken lines.

user13253
  • 8,666
  • 2
  • 42
  • 65

1 Answers1

3

I believe you should use streams:

stream = OpenWrite["wraptest.m", PageWidth -> Infinity];

Do[PutAppend[Table[i, {RandomInteger[100]}], stream], {i, 100}]

You can also use Write in place of PutAppend:

 Do[stream ~Write~ Table[i, {RandomInteger[100]}], {i, 100}]

Be sure to Close your stream when you are done:

Close[stream];
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    It's well acceptable. The documentation doesn't say the second argument can be a stream expression. – user13253 Apr 24 '12 at 04:26
  • If I have a file written in my original way and have some long lines wrapped, how to amend it so each line has exactly one expression (List in this case) without rewriting the large file? – user13253 Apr 24 '12 at 04:27
  • @Problemania I didn't check the documentation before posting; I thought it was in there but it seems not. As for fixing your existing file my first inclination is to read in the data and write it to a new file. If memory usage is your concern you can do it line by line with Read and Write. – Mr.Wizard Apr 24 '12 at 04:33
  • Writing those files took hours. To fix the line breaks, I would have to basically rewrite them? It doesn't have to use Mathematica if there is better way to do it. It seems the broken long line always has the second and additional lines start with white spaces. Maybe that could be exploited? – user13253 Apr 24 '12 at 04:44
  • @Problemania well yes, I suppose it could. I thought you were asking for a Mathematica solution. A text editor or command line utility (not my specialty) could indeed do this faster I expect. At least in the sample given the lines that wrap also end with a space. If this holds true for your actual data the replacement should be easy enough in any reg-ex replacement utility. – Mr.Wizard Apr 24 '12 at 04:49
  • 1
    I find a way to fix the long lines with sed (see in OP). – user13253 Apr 24 '12 at 05:45
  • The problem with the additional OpenWrite is that it roughly doubles writing time compared to using Put/PutAppend only (at least in my case). Is there no wrapper that can be used inside Put specifying line width? – István Zachar Nov 08 '13 at 16:23
  • @István I'll try to take another look at it if I have time. (You may have noticed I've been taking an extended break from the site.) Consider however that options such as line width might be the reason why OpenWrite is slower than Put. – Mr.Wizard Nov 09 '13 at 03:20