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.

I have two lists:

{a, 1, b} and {b, 1.3, y}

My goal is to print them to file test.dat, so that the content of test.dat looks like:

(a 1 b)
(b 1.300 y)

I want the elements of the lists in the output file to be separated by spaces.

Questions:

  1. How can I do it?
  2. Is it possible to use Print command to do it?

Edit 1:

I was going along these lines:

myPrint[list_] := Print["(", Row[list, " "], ")"]
l1 = {a, 1, b};
myPrint[l1]

(a 1 b)

My problem is that I don't know how to export the results I got to a file. So this is the reason for my question 2.

Edit Final: Problem Solved

Solution by swish (from comments)

str = OpenWrite["test.dat"]
myPrint[list_] :=
  WriteString[str, Row[{"(", Row[NumberForm[#, {4, 3}] & /@ list, " "], ")\n"}]]
l1 = {a, 1, b}
myPrint[l1]
share|improve this question
    
You've seen Export[]? –  J. M. Apr 18 '13 at 11:05
    
@J.M., thank you for your comment. Yes, I did, but it has some predefined format of output (for example, column output - each element of the list is being output on a new line). What I want is control over the output format. I got the idea how to get this control using Print function, but have no clue how to output (Export?) results of the Print function to file. –  user1541776 Apr 18 '13 at 15:12
add comment

3 Answers

up vote 5 down vote accepted

Some tweaks to your function and you done:

myPrint[list_] := 
 ToString[Row[{"(", Row[NumberForm[#, {4, 3}] & /@ list, " "], ")"}]]

Now it returns string and you can Export it.

share|improve this answer
    
thank you very much! This solves my problem! Still, I've got one more question: I read that Print outputs to \$Output. It seems that if I set \$Output to some file, Print will do the job without necessity to transform output into String. Does anyone know how to set \$Output to output to file test.dat? –  user1541776 Apr 18 '13 at 15:37
    
@user1541776 Use OpenWrite to open your file and Write to it instead of Print. –  swish Apr 18 '13 at 15:46
    
thank you once again! The problem is that it outputs into file "Row[{"(", Row[{NumberForm[a, {4, 3}], NumberForm[1, {4, 3}], NumberForm[b, {4, 3}]}, " "], ")"}]" rather than (a 1 b), i.e. in some sense outputs the expression and not the result of the evaluation of expression. (Tried to add Evaluat[] - it didn't help). –  user1541776 Apr 18 '13 at 15:53
    
@user1541776 WriteString then :). –  swish Apr 18 '13 at 15:55
    
thank you, it worked! You made my day :-) –  user1541776 Apr 18 '13 at 17:26
show 2 more comments

A solution based on StringReplace and Export and with number format control:

x = {a, 1, b}
z = {b, 1.3, y, 4}

(* replace brackets and braces *)
   sr[x_] := StringReplace[
  ToString@(x /. y_?NumericQ -> NumberForm[y, {3, 4}]), {","->" ", "{"->"(", "}"->")"}]

(* Export a collection of your expressions to a file *)
Export["/tmp/test.tmp", sr /@ {x, z, x, z}, "Table"]
share|improve this answer
add comment

Here's a simple and inelegant way. Define a function that places the information in the desired text-string form.

string[x_]:="("~~ToString[x[[1]]]~~" "~~ToString[x[[2]]]~~" "~~ToString[x[[3]]] ~~ ")"

and then Export

 Export["test.txt", {string[x], string[z]}]

where

x = {a, 1, b}
z = {b, 1.3, y}
share|improve this answer
    
thank you for your answer. There are two things I need: 1. I would like to print lists with arbitrary number of elements (not necessarily 3 as I specified in my example) 2. I would like to have more control over printed numbers - floats to be output in %f4.3 format rather than %f2.1. –  user1541776 Apr 18 '13 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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