How to break outer loops from inner structures that respond break (loops/switch)

Swift allows for labeled statements. Using a labeled statement, you can specify which which control structure you want to break from no matter how deeply you nest your loops (although, generally, less nesting is better from a readability standpoint). This also works for continue. Example: outerLoop: while someCondition { if someOtherCondition { switch (someValue) { … Read more

How to render a tree in Twig

I played around with domi27’s idea and came up with this. I made a nested array as my tree, [‘link’][‘sublinks’] is null or another array of more of the same. Templates The sub-template file to recurse with: <!–includes/menu-links.html–> {% for link in links %} <li> <a href=”https://stackoverflow.com/questions/8326482/{{ link.href }}”>{{ link.name }}</a> {% if link.sublinks %} … Read more

Get iteration index from List.map()

To get access to index, you need to convert your list to a map using the asMap operator. Example final fruitList = [‘apple’, ‘orange’, ‘mango’]; final fruitMap = fruitList.asMap(); // {0: ‘apple’, 1: ‘orange’, 2: ‘mango’} // To access ‘orange’ use the index 1. final myFruit = fruitMap[1] // ‘orange’ // To convert back to … Read more

For Loop on Lua

Your problem is simple: names = {‘John’, ‘Joe’, ‘Steve’} for names = 1, 3 do print (names) end This code first declares a global variable called names. Then, you start a for loop. The for loop declares a local variable that just happens to be called names too; the fact that a variable had previously … Read more

Python loop counter in a for loop [duplicate]

[*] Use enumerate() like so: def draw_menu(options, selected_index): for counter, option in enumerate(options): if counter == selected_index: print ” [*] %s” % option else: print ” [ ] %s” % option options = [‘Option 0’, ‘Option 1’, ‘Option 2’, ‘Option 3’] draw_menu(options, 2) Note: You can optionally put parenthesis around counter, option, like (counter, option), … Read more