Thanks for answering the previous question had before, i did the following, but i am trying to execute this loop, but there is no error given. I am trying to do date difference.
In[7] = Import["testA.txt", "Table" , "HeaderLines" -> 1]
Out[7] = {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}
In[10] = For[i = 1, i < 4,
i = i + 1, {a = Out[7] [[i, 2]], b = Out[7] [[i, 3]],
c = Out开发者_Python百科[7] [[i, 4]] , d = Out[7][[i, 5]], e = Out[7][[i, 6]],
f = Out[7][[i, 7]], DateDifference[{a, b, c}, {d, e, f}]}]
Yes. For[] doesn't generate an output. The differences you wanted to computer were computed (191, 135, and 69) and the results weren't written or stored anywhere. To make this evident, rewrite your DateDifference[] call to Print[DateDifference[{a,b,c},{d,e,f}]];
.
Without any further hints about what you wanted to happen. It's not clear how to provide any more advice. You could Sow[] and Reap[]. You could Join[] the result of DateDifference[] to a results List. You could print the difference (as described above). You could assign the results to some variable(s), define a symbol to take their values of specific inputs, et c., et c., et c...
-- EDIT --
Oh, and to address some of the other respondents, the correct form of this code is:
foo = Import["testA.txt", "Table" , "HeaderLines" -> 1]; diff[in_List] := Join[in[[Range[2, 7]]], {DateDifference[in[[Range[2, 4]]], in[[Range[5, 7]]]]}] diff /@ foo
Output is: {{2010, 2, 20, 2010, 8, 30, 191}, {2010, 4, 30, 2010, 9, 12, 135}, {2010, 8, 20, 2010, 10, 28, 69}}
(and you probably want to assign that somewhere also).
Since, you seem to want to process each list element individually, I'd use Map
(or its short form /@
) for this, as follows.
In[1]:=data = {{100, 2010, 2, 20, 2010, 8, 30},
{110, 2010, 4, 30, 2010, 9, 12},
{112, 2010, 8, 20, 2010, 10, 28}};
#~Join~{DateDifference[#[[2;;4]],#[[5;;]]]}& /@ data
Out[1]:={191, 135, 69}
Mathematica is primarily a functional programming language, so while constructs like For
exist, it is usually better to think in terms of functions operating on and transforming expressions. Often, if you need a procedural process like For
, Table
is a better bet.
My code has one slight disadvantage, the lack of named variables can be a pain sometimes. In this case, however, they are not necessary. But, if you need them there are several constructs that could be used With
, Block
, Module
, or even Function
.
Warning: This is not the way to go
This is your program corrected and running. I am posting it because I saw may conceptual errors in your code and I thought that having a running version for comparing it with yours may help you to identify them and learn.
Some points:
- Use of common constructs (Length@x, i++, etc)
- Don't use "Out[n]=" ... Never use capitals as first char of a variable name
- Learn how to construct lists (see the AppendTo)
Here is a running code:
x = Import["testA.txt", "Table" , "HeaderLines" -> 1]
ret = {};
For[i = 1, i <= Length@x, i++,
AppendTo[ret,
{a = x[[i, 2]],
b = x[[i, 3]],
c = x[[i, 4]],
d = x[[i, 5]],
e = x[[i, 6]],
f = x[[i, 7]],
DateDifference[{a, b, c}, {d, e, f}]}
];
];
Print@ret
But as I said THIS IS NOT THE WAY TO GO. Read and study (and run in debug!) rcollyer's answer for understanding how to do it right in Mathematica.
BTW, You could replace the For loop by a Table (see rcollyer's suggestion)
Table[
{a = x[[i, 2]],
b = x[[i, 3]],
c = x[[i, 4]],
d = x[[i, 5]],
e = x[[i, 6]],
f = x[[i, 7]],
DateDifference[{a, b, c}, {d, e, f}]}, {i, Length@x}]
精彩评论