while-loop

Grouping records from while loop | PHP

扶醉桌前 提交于 2019-12-01 22:49:32
I'm trying to group down records by their priority levels, e.g. --- Priority: High --- Records... --- Priority: Medium --- Records... --- Priority: Low --- Records... Something like that, how do I do that in PHP? The while loop orders records by the priority column which has int value (high = 3, medium = 2, low = 1). e.g. WHERE priority = '1' The label: "Priority: [priority level]" has to be set above the grouped records regarding their level EDIT: <?php while($row = mysql_fetch_array($result)) { echo '<h1>Priority Level: ' . $row['priority'] . '</h1>'; echo $row['name']; } ?> Like that piece

Trying to find vowels of a string using Ruby while loops

痴心易碎 提交于 2019-12-01 21:45:06
def count_vowels(string) vowels = ["a", "e", "i", "o", "u"] i = 0 j = 0 count = 0 while i < string.length do while j < vowels.length do if string[i] == vowels[j] count += 1 break end j += 1 end i += 1 end puts count end I'm having trouble spotting where this goes wrong. If this program encounters a consonant, it stops. Also, how would the same problem be solved using the ".each" method? The problem is that you never reset j to zero. The first time your outer while loop runs, which is to compare the first character of string to each vowel, j is incremented from 0 (for "a") to 4 (for "u"). The

Usage of defined with Filehandle and while Loop

依然范特西╮ 提交于 2019-12-01 21:33:38
While reading a book on advanced Perl programming (1) , I came across this code: while (defined($s = <>)) { ... Is there any special reason for using defined here? The documentation for perlop says: In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly: [...] So,

What is the difference between while (x = false) and while (!x) in Java?

陌路散爱 提交于 2019-12-01 21:18:10
Sorry, I'm new to Java, so this question might be unclear. I have been recently dealing with enclosing a try and catch statement in a while loop, because I wanted to make sure that getting input was enclosed from the rest of the program. I have come across a problem where using an exclamation mark (!) in front of a variable in the while conditions (e.g. while (!done)) instead of using = false (e.g. while (done = false)) changes the way my program runs. The former (!done) results in the try and except statements running as expected. The latter (done = false) does not, simply skipping them and

Shell script while read line loop stops after the first line

人盡茶涼 提交于 2019-12-01 20:26:38
I have the following shell script. The purpose is to loop thru each line of the target file (whose path is the input parameter to the script) and do work against each line. Now, it seems only work with the very first line in the target file and stops after that line got processed. Is there anything wrong with my script? #!/bin/bash # SCRIPT: do.sh # PURPOSE: loop thru the targets FILENAME=$1 count=0 echo "proceed with $FILENAME" while read LINE; do let count++ echo "$count $LINE" sh ./do_work.sh $LINE done < $FILENAME echo "\ntotal $count targets" In do_work.sh , I run a couple of ssh commands

How can I remove the last comma from a loop in C++ in a simple way?

本小妞迷上赌 提交于 2019-12-01 20:02:01
This program is for printing prime numbers till the input given and separating every prime number with a comma. void main(){ int N, counter=0, isPrime; int k, j; cout << "Enter maximum range: "; cin >> N; for (j=2; j<=N; j++){ isPrime = 0; k = 2; while (k<j){ if (j%k==0){ isPrime++; } k++; } if (isPrime==0){ if (k==N){ cout << j; } else{ cout << j << ","; } counter++; } } cout << endl; system("pause"); } It is only removing the last comma for prime number inputs, not for any other input. How can I fix this? Input: 23 Output: 2,3,5,7,11,13,17,19,23 Input: 8 Output: 2,3,5,7, Input: 9 Output: 2,3

Looping statements performance and pre-allocating the looping statement itself

南楼画角 提交于 2019-12-01 18:36:36
问题 This observation is not that important, because the time performance wasted on the loop statements will probably be much higher than the looping itself. But anyway, I will share it since I searched and couldn't find a topic about this. I always had this impression that pre-allocating the array I would loop, and then loop on it, would be better than looping directly on it, and decided to check it. The code would be to compare the efficiency between this two fors: disp('Pure for with column on

Any reason to replace while(condition) with for(;condition;) in C++?

∥☆過路亽.° 提交于 2019-12-01 18:23:35
问题 Looks like while( condition ) { //do stuff } is completely equivalent to for( ; condition; ) { //do stuff } Is there any reason to use the latter instead of the former? 回答1: There's no good reason as far as I know. You're intentionally misleading people by using a for-loop that doesn't increment anything. Update: Based on the OP's comment to the question, I can speculate on how you might see such a construct in real code. I've seen (and used) this before: lots::of::namespaces::container:

Python&PyGTK: Stop while on button click

风流意气都作罢 提交于 2019-12-01 18:23:27
I'm working on programming some application and I would like to create while loop when button is clicked and if it's clicked again to stop it. This is the code for button: self.btnThisOne = gtk.Button("This one") self.btnThisOne.connect("clicked", self.startLoop) The code for startLoop def would be: def startLoop(self): while self.btnThisOne?(is_clicked)?: #do something How to do that? Unfortunately, you cannot just have an unconstrained while loop running in the main thread of your application. That would block the main gtk event loop and you won't be able to process any more events. What you

Why is While (rs.next()) statement ending after 1st iteration?

血红的双手。 提交于 2019-12-01 18:22:26
I am using a SELECT statement to get data from a table and then insert it into another table. However the line "stmt.executeQuery(query);" is inserting the first line from the table then exits. When I comment this line out, the while loop loops through all the lines printing them out. The stacktrace isn't showing any errors. Why is this happening? try{ String query = "SELECT * FROM "+schema_name+"."+table; rs = stmt.executeQuery(query); while (rs.next()) { String bundle = rs.getString("BUNDLE"); String project_cd = rs.getString("PROJECT_CD"); String dropper = rs.getString("DROPPER"); String