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'm new in Mathematica and I am trying to write numbers from 1 to 10 in txt file. But "Null" is everything that is written in my file. The code is:

Export["C:\\Users\\Sealy\\Desktop\\list.txt",
       For[k = 1, k <= 10, k++, Print[List[k]]]]
share|improve this question
1  
Have you seen this? –  belisarius Sep 4 '12 at 15:06
1  
@stevenvh It's the name that halted me. =:-O –  Mr.Wizard Sep 15 '12 at 14:04
add comment

4 Answers

Here are some variants to achieve what you want using various notations and avoiding the use of the dreaded For loop.

Exporting as strings

Standard notation:

Export[ "/tmp/10.txt", Range@10]

Infix notation:

 "/tmp/10.txt"~Export~Range@10

Exporting in Mathematica format

Save as a list, so it can easily be read back into mathematica using Get.

Range@10 >> "/tmp/10.mma"

Read it back in using the short form of the function Get:

<< "/tmp/10.mma"

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Why you might not want a For loop

For loops aren't used very much in Mathematica as there are more efficient ways to approach the need to repeat computations. Lists are fundamental to what Mathematica does and in conjunction with other techniques such as Map make For loops and Do,While redundant.

Map

The Map function, shortform /@, which can apply a function to a list of items.

Sin/@{1,2,3,4,5,6} - > {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}

Listability

The ability of functions to apply themselves to a list,

Sin[{1,2,3,4,5,6}] -> {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}

Functions can Return Lists

( Here we use the short form of function invocation, f@arg instead of f[arg] )

Range@6 -> {1,2,3,4,5,6}

and

Sin@Range@6-> {Sin[1], Sin[2], Sin[3], Sin[4], Sin[5], Sin[6]}

share|improve this answer
add comment

Recommendation: Read up on Table, Range etc. . Press F1 often!

Export[NotebookDirectory[] <> "test.txt", Range[10]]
share|improve this answer
add comment

Print is used for it's side effect (which is printing to the screen) but it returns Null. Also the For loop returns Null

Try this:

lst = {};
For[k = 1, k <= 10, k++, AppendTo[lst, {k}]]
lst
(* {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}} *)
Export["C:\Users\Sealy\Desktop\list.txt", lst]
share|improve this answer
    
I have managed to export lists and to import them in Mathematica using ways you all suggested. but now, I have to sum two elements of those imported lists (for example, for {d_1, d_2, d_3} I have to find sums d_1+d_2, d_2+d_3 and d_1+d_3 and to deal with those sums that satisfy some conditions). How can I do that? I saw a lot of examples that use "#" as a variable of a list but I think I cannot use that two times (For example, If[#+#], Print[12] does not make sense)? Thank you very much! –  WayneGacy Sep 4 '12 at 10:20
    
@WayneGacy, that's a new question. Function[in, in + RotateLeft[in]][{d1, d2, d3}] –  user21 Sep 4 '12 at 10:24
    
Ups, thank you! –  WayneGacy Sep 4 '12 at 10:27
add comment

Try

(* Initiate an empty List *)
list = {};
(* Use AppendTo in order to add  the entries in each iteration *)
For[k = 1, k <= 10, k++, list = AppendTo[list, k]];
(* Then export the List *)
Export["C:\Users\Sealy\Desktop\list.txt", list]

However the simplest Mathematica way to do it will be by calling Table

list=Table[k,{k,1,10}];
Export["C:\Users\Sealy\Desktop\list.txt", list]

BR

share|improve this answer
    
Thank you very, very much! –  WayneGacy Sep 4 '12 at 9:45
1  
Table[k,{k,1,10}] is waaay too long. Range[10] does exactly the same. –  stevenvh Sep 4 '12 at 10:35
2  
@stevenvh this was meant to introduce the very commonplace and still powerful MMA function Table to the OP as he claims to be a novice. I thought of explainign him a bit more in detail but image_dioctor has already done that... –  PlatoManiac Sep 4 '12 at 11:27
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.