Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 6, 2014 #1
    When I use the code directly below, it works (indicated by the "Out:...").
    Code (Text):
    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 (Text):
    For[j = 0, j <= 5, j++,
    tmp2 = Flatten[{33, {1, 2 + j, 6}, 55}]

    (*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.
  2. jcsd
  3. Dec 6, 2014 #2


    Staff: Mentor

    It looks like you forgot a semicolon after the call to Flatten.
  4. Dec 6, 2014 #3
    Sigh. You are correct, thank you very much.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook