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

  • Context: Mathematica 
  • Thread starter Thread starter teroenza
  • Start date Start date
  • Tags Tags
    List Loop Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 1K views
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
Sigh. You are correct, thank you very much.