What’s the difference between iterating over a file with foreach or while in Perl?

For most purposes, you probably won’t notice a difference. However, foreach reads each line into a list (not an array) before going through it line by line, whereas while reads one line at a time. As foreach will use more memory and require processing time upfront, it is generally recommended to use while to iterate … Read more

random iteration in Python

You can use random.shuffle() to, well, shuffle a list: import random r = list(range(1000)) random.shuffle(r) for i in r: # do something with i By the way, in many cases where you’d use a for loop over a range of integers in other programming languages, you can directly describe the “thing” you want to iterate … Read more

Javascript: hiding prototype methods in for loop?

You can achieve desired outcome from the other end by making the prototype methods not enumerable: Object.defineProperty(Array.prototype, “containsKey”, { enumerable: false, value: function(obj) { for(var key in this) if (key == obj) return true; return false; } }); This usually works better if you have control over method definitions, and in particular if you have … Read more

tech