for-loop

PHP: loop to create list of previous 12 months

只愿长相守 提交于 2020-01-11 07:39:05
问题 is there a way I can use a PHP loop to create a list like the following with the previous 12 months, based on the current month (excluding the current month) ? The value should always be the first of the month (format: yyyy-mm-dd) and the dropdown itself should just show year and month (format: yyyy-mm): <option value="2014-03-01">2014-03</option> <option value="2014-02-01">2014-02</option> <option value="2014-01-01">2014-01</option> <option value="2013-12-01">2013-12</option> <option value=

Is it possible to create n number of objects in java using a for loop? [duplicate]

一曲冷凌霜 提交于 2020-01-11 07:11:09
问题 This question already has answers here : Assigning variables with dynamic names in Java (7 answers) Closed 4 years ago . For example; i'm using this class: Point originOne = new Point(x, y); If i want to create a N number of points (originTwo,originThree...originN); can I do it using a for loop like : for(int i=0;i<n-1;i++){ } If it's possible; how do i give them different names? 回答1: You could put them into an array. Point[] origin = new Point[n]; for (int i = 0; i < n; i++) { origin[i] =

Extract values by key from a nested dictionary

天涯浪子 提交于 2020-01-11 06:59:52
问题 Given this nested dictionary, how could I print all the "phone" values using a for loop? people = { 'Alice': { 'phone': '2341', 'addr': '87 Eastlake Court' }, 'Beth': { 'phone': '9102', 'addr': '563 Hartford Drive' }, 'Randy': { 'phone': '4563', 'addr': '93 SW 43rd' } 回答1: for d in people.values(): print d['phone'] 回答2: Loop over the values and then use get() method, if you want to handle the missing keys, or a simple indexing to access the nested values. Also, for the sake of optimization

C++0x way to replace for(int i;;) range loops with range-based for-loop

走远了吗. 提交于 2020-01-11 02:53:04
问题 So I've been getting into the new C++ using GCC 4.6 which now has range-based for-loops. I've found this really nice for iterating over arrays and vectors. For mainly aesthetic reasons I wondered if there was a way to use this to replace the standard for(int i = min; i < max; i++) {} with something like for(int& i : std::range(min, max)) {} Is there something natively built into the new C++ standard that allows me to do this? Or do I have to write my own range/iterator class? 回答1: I don't see

Sending post request in for loop

陌路散爱 提交于 2020-01-11 02:31:11
问题 I would like to send post requests in loop. If i send for example 2 requests in a row only the last request really made the callback. What am i do wrong? this.assignAUnits = function(){ var currentIncidentId = this.incident.incidentId; for (var i=0; i< this.selectedAvailableUnits.length; i++){ var unit = this.selectedAvailableUnits[i]; var unitId = unit.unitId; var url = '/incident/' + currentIncidentId + '/assignUnit/' + unitId $http.post(url).then(function(response) { DOING SOMETHING },

Perform pairwise comparison of matrix

南楼画角 提交于 2020-01-10 20:18:51
问题 I have a matrix of n variables and I want to make an new matrix that is a pairwise difference of each vector, but not of itself. Here is an example of the data. Transportation.services Recreational.goods.and.vehicles Recreation.services Other.services 2.958003 -0.25983789 5.526694 2.8912009 2.857370 -0.03425164 5.312857 2.9698044 2.352275 0.30536569 4.596742 2.9190123 2.093233 0.65920773 4.192716 3.2567390 1.991406 0.92246531 3.963058 3.6298314 2.065791 1.06120930 3.692287 3.4422340 I tried

Why does a range-based for statement take the range by auto&&?

三世轮回 提交于 2020-01-10 17:48:40
问题 A range-based for statement is defined in §6.5.4 to be equivalent to: { auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } } where range-init is defined for the two forms of range-based for as: for ( for-range-declaration : expression ) => ( expression ) for ( for-range-declaration : braced-init-list ) => braced-init-list (the clause further specifies the meaning of the other sub

'for' loop vs Qt's 'foreach' in C++

只愿长相守 提交于 2020-01-09 15:03:10
问题 Which is better (or faster), a C++ for loop or the foreach operator provided by Qt? For example, the following condition QList<QString> listofstrings; Which is better? foreach(QString str, listofstrings) { //code } or int count = listofstrings.count(); QString str = QString(); for(int i=0;i<count;i++) { str = listofstrings.at(i); //Code } 回答1: It really doesn't matter in most cases. The large number of questions on StackOverflow regarding whether this method or that method is faster, belie

Use a variable in a 'for' loop

◇◆丶佛笑我妖孽 提交于 2020-01-09 11:24:54
问题 I have following code: @echo off SET ITER=0 for %%i in (%*) do ( SET ITER+=1 ECHO %ITER% ) The output is (for three arguments): 0 0 0 Expected output: 1 2 3 Why can't I access the updated variable in the for loop? 回答1: Expansion of variables with percents is done before a statement/block is executed. So in your case the complete block is expanded before the echo %ITER% is executed, to constant echo 0 . The variable ITER itself is updated in the loop properly. To avoid this, you could use the

Use a variable in a 'for' loop

情到浓时终转凉″ 提交于 2020-01-09 11:24:06
问题 I have following code: @echo off SET ITER=0 for %%i in (%*) do ( SET ITER+=1 ECHO %ITER% ) The output is (for three arguments): 0 0 0 Expected output: 1 2 3 Why can't I access the updated variable in the for loop? 回答1: Expansion of variables with percents is done before a statement/block is executed. So in your case the complete block is expanded before the echo %ITER% is executed, to constant echo 0 . The variable ITER itself is updated in the loop properly. To avoid this, you could use the