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.

My code is: Do[Print[number[[i ;; (i + 4)]]], {i, number}]. The variable number here is a list consisting of 1000 digits.

My aim is to Print[] a series of lists as follows: the first list consists of items 1 through 5 from number. The second list consists of items 2 through 6 of number, and so on.

Instead, what I'm getting is this: enter image description here

You can see that it does Print some lists, but it seems to be shuffling the digits around in some random kind of order, plus completely making up some numbers (e.g. where did the '5' in the first list come from??).

Then suddenly it grinds to a halt and displays an error message saying 'Cannot take positions 0 through 4 in {the list}, before throwing up the entire 1000 digit list, and then continuing with further 5-member lists and repeating the pattern.

I have no idea what's happened here so I'd be very grateful if somebody could explain to me what's going on - and more importantly, how I can get this code to work as intended.


share|improve this question

closed as off-topic by Pickett, bobthechemist, Michael E2, m_goldberg, Mr.Wizard Jan 3 '14 at 8:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Pickett, bobthechemist, Michael E2, m_goldberg, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

Try Length[number] instead of number. –  ybeltukov Jan 3 '14 at 2:33
Or Length[number]-4 –  Pickett Jan 3 '14 at 2:35
Amazing, it worked! I don't understand why though. Why didn't the original code simply loop through all the characters in number? And how was that remedied by specifying Length instead? –  Aron Jan 3 '14 at 2:39
Have you looked at the built-in function Partition? I it provide an easier way to do what you are describing. –  m_goldberg Jan 3 '14 at 2:41
Do[f[i],{i,{5,7,3}}] is f[5]; f[7]; f[3]; whereas Do[f[i],{i,Length@{5,7,3}}] is f[1]; f[2]; f[3]; so they are two very different things. –  Pickett Jan 3 '14 at 2:49

2 Answers 2

I think this will do what you want, and it's both easier to write and more efficient than your approach.

SeedRandom[42]; digits = RandomInteger[{0, 9}, 20]
{6, 0, 8, 2, 0, 0, 0, 6, 4, 1, 1, 8, 0, 3, 3, 5, 5, 8, 9, 8}
Partition[digits, 4, 1] // TableForm


share|improve this answer

Just to answer your specific question: this issue is your iterator should by {j,1,2,...Length@number-4}. What your could is doing is substituting the element of "number" in i and if this is not an integer fails and in general will not relate to position in list. I agree with comments and m_goldberg that Partition does what you want. Test the following for yourself:

Test data:

number = RandomInteger[{0, 9}, 20];

Modification of your code:

Do[Print[number[[j ;; j + 4]]], {j, Length@number - 4}]


Partition[number, 5, 1]
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.