Mathematica Mathematica: Getting part of a list, lithin a loop.

AI Thread Summary
The discussion focuses on an issue encountered when using the Flatten function within a loop in a programming context. Initially, the code works correctly outside the loop, producing the expected output. However, when placed inside a For loop, an error arises indicating that the part specification is longer than the depth of the object. This confusion is resolved when a participant points out that a missing semicolon after the Flatten call may be the cause of the problem. The resolution highlights the importance of syntax accuracy in programming, particularly when working with nested structures and loops.
teroenza
Messages
190
Reaction score
5
When I use the code directly below, it works (indicated by the "Out:...").
Code:
tmp2 = Flatten[{33, {1, 2 + j, 6}, 55}];(*Out:{33,1,2+j,6,55} *)

tmp2[[4]]; (*Out: 6 *)

But when I use it inside a loop, it acts like the tmp2 list is not the correct size:
Code:
For[j = 0, j <= 5, j++,
tmp2 = Flatten[{33, {1, 2 + j, 6}, 55}]
   Print[tmp2[[4]]]
]

(*Out: Part::partd: Part specification tmp2[[4]] is longer than depth of object. >>*)

This is a minimal example of a larger piece, but I don't see why it's not working.
 
Physics news on Phys.org
It looks like you forgot a semicolon after the call to Flatten.
 
  • Like
Likes teroenza
Sigh. You are correct, thank you very much.
 

Similar threads

Replies
6
Views
4K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
22
Views
3K
Replies
6
Views
6K
Replies
1
Views
2K
Back
Top