Looping through a listHi, I have a list of month names and I want to loop though the list starting a a specific month let's say June (the 6th month) and going backwards towards January.
MonthList ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//total number of months
nMonths = N Items (MonthList);
//initialize list of selected months
ElevenMo = {};
For (i = 6, i <= 11, i -= 1,
If (i >= 1, Continue () );
nextMonth = MonthList[i];
Insert Into (ElevenMo, nextMonth[i]),
);I think I have the basics but my loop keeps going in an infinite loop. I will appreciate your advice on how to fix it. And yes, at some point the loop has to go back to December, but that will be another battle.
ScriptingRe: Looping through a listHere are 2 ways to do this.......I took the liberty to add the ability to determine the starting value from the name of the month you want to start from. Both examples give the same results
Names Default To Here( 1 );
MonthList = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
//total number of months
nMonths = N Items( MonthList );
targetMonth = "June";
startMonth = contains( MonthList, targetMonth );
//initialize list of selected months
ElevenMo = {};
For( i = startMonth, i >= 1, i--,
Insert Into( ElevenMo, MonthList[i] );,
);
Names Default To Here( 1 );
MonthList = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
//total number of months
nMonths = N Items( MonthList );
targetMonth = "June";
startMonth = contains( MonthList, targetMonth );
ElevenMo = MonthList[1::startMonth]Re: Looping through a list This should work
MonthList ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//total number of months
nMonths = N Items (MonthList);
//initialize list of selected months
ElevenMo = {};
For (i = 6, i > 0, i--,
nextMonth = MonthList[i];
Insert Into (ElevenMo, nextMonth),
);Re: Looping through a listHi TX and ThuongLe, thank you very much for your answers. They are also great lessons. I love the elegance of the StartMonth and the TargetMonth.