3
$\begingroup$
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.

$\endgroup$
1
  • $\begingroup$ Strongly related: (9977). $\endgroup$ Commented Jul 18, 2015 at 9:32

1 Answer 1

3
$\begingroup$

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];
$\endgroup$
8
  • 1
    $\begingroup$ It's well acceptable. The documentation doesn't say the second argument can be a stream expression. $\endgroup$
    – user13253
    Commented Apr 24, 2012 at 4:26
  • $\begingroup$ 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? $\endgroup$
    – user13253
    Commented Apr 24, 2012 at 4:27
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Commented Apr 24, 2012 at 4:33
  • $\begingroup$ 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? $\endgroup$
    – user13253
    Commented Apr 24, 2012 at 4:44
  • 1
    $\begingroup$ I find a way to fix the long lines with sed (see in OP). $\endgroup$
    – user13253
    Commented Apr 24, 2012 at 5:45

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.