while-loop

How to iterate through string one word at a time in zsh

半腔热情 提交于 2019-12-06 16:50:34
问题 How do I modify the following code so that when run in zsh it expands $things and iterates through them one at a time? things="one two" for one_thing in $things; do echo $one_thing done I want the output to be: one two But as written above, it outputs: one two (I'm looking for the behavior that you get when running the above code in bash) 回答1: In order to see the behavior compatible with Bourne shell, you'd need to set the option SH_WORD_SPLIT : setopt shwordsplit # this can be unset by

Python syntax for an empty while loop

泪湿孤枕 提交于 2019-12-06 16:34:59
问题 I have written this: while file.readline().startswith("#"): continue But I suspect the continue is unnecessary? What is the correct syntax for what i'm trying to achieve? 回答1: while file.readline().startswith("#"): pass This uses the pass statement : The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. http://www.network-theory.co.uk/docs/pytut/passStatements.html 来源: https://stackoverflow.com/questions/14797046/python

The equivalent C code of a valid C++ code with a While loop does not compile

梦想的初衷 提交于 2019-12-06 16:26:13
问题 The following code containing a while loop compiles in C++. #include <iostream> using namespace std; int main() { while (int i = 5) { break; } return 0; } However, the following equivalent C code results in an error if compiled in C: #include <stdio.h> int main() { while (int i = 5) { break; } return 0; } Compiler output: > prog.c: In function 'main': prog.c:5:9: error: expected expression > before 'int' while (int i = 5)prog.c: In function 'main': > prog.c:5:9: error: expected expression

cin >> integer and while loop

隐身守侯 提交于 2019-12-06 16:20:55
With below code, if I enter a letter or a really long number, the while loop will go haywire, why is that? void main() { int n{ 0 }; while (true) { cout << "Enter a number: "; cin >> n; cout << n << endl; } } The problem is that operator>> is expecting to draw an integer off of the input stream, but there is something else sitting there (the non-integer that the user typed). This sets an error state on the input stream. In this state, the cin >> ... construct no longer blocks for input because there's already something (not an integer) in the stream, so you see your loop go haywire. What needs

Loop through rows in an excel worksheet and copy a range if the cell isn't blank

拜拜、爱过 提交于 2019-12-06 14:59:54
I have virtually no VBA experience except that, from what I have seen other spreadsheets do, I am convinced this must be possible. I have searched all over but cannot find any explanations to help or code I can just use. I hope someone can help. I have a download from our website cart which does not format the data how it needs to be to then up-load into some new sales order/invoice generating software. As an example here is a link to an image that shows how the data currently looks(the workbook is called 'Orders.csv' but I can convert to xlsx if needed): http://web225.extendcp.co.uk/fiercepc

Validating input with while loop and scanner

无人久伴 提交于 2019-12-06 14:50:02
问题 What's the best way about getting a valid integer from a user that is in a specified range (0,20) and is an int . If they enter an invalid integer print out error. I am thinking something like: int choice = -1; while(!scanner.hasNextInt() || choice < 0 || choice > 20) { System.out.println("Error"); scanner.next(); //clear the buffer } choice = scanner.nextInt(); Is this correct or is there a better way? 回答1: Where do you change choice inside of your while loop? If it's not changed you can't

Looping through MySQL left join in php vs. 2 separate queries

流过昼夜 提交于 2019-12-06 14:26:12
问题 I have a simple question and answer section on my website that allows comments. I currently populate the answers using a loop and a $_GET['id'] value. Here is my query <?php try{ $parent=$_GET['id']; $post_type = "2"; $stmt = $dbh->prepare( "SELECT p.post_id, p.content, p.author, p.created, pc.comment_id AS comment_id, pc.content AS comment_content FROM posts AS p LEFT JOIN posts_comments AS pc ON p.post_id = pc.parent_id WHERE p.post_type = :post_type AND p.parent = :parent "); $stmt-

C++, Real-Time User Input, during While Loop

巧了我就是萌 提交于 2019-12-06 14:20:02
问题 Is there was any way for the User to give a Real-Time input, while something is constantly being updated in the background. Basically, making the program not stop, when asking for user input. For example, It will ask for user input, while a number is constantly being calculated. 回答1: There are two ways of this problem, as I see it. One is, as xebo commented, using multi threading. Use one thread for the constant calculation of the number or whatever, and another thread to look for user input

How to count occurrence of each character in java string using Pattern Class(Regex)

走远了吗. 提交于 2019-12-06 13:57:39
问题 I am trying to find a number of Occurrence of each character on the given string. Expected output: t=2 e=1 s=1 i=1 n=1 g=1 Current output: T=0 e=0 s=0 t=0 i=0 n=0 g=0 Code: String str = "Testing"; int count = 0; Pattern p = Pattern.compile("[a-zA-Z]", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str); while (m.find()) { if (m.group().equals(str)) { count++; } System.out.println(m.group() + "=" + count); } There are many ways of doing this but I am looking for Regex only, so how can we

SQL query not displaying the first result?

筅森魡賤 提交于 2019-12-06 13:49:32
I am trying to dynamically create a navigation menu in my php page. I have a query to create a list of the active pages but for some reason the first result never shows $menu = mysql_query("SELECT link FROM myTable WHERE active_page='y' ORDER BY menu_order"); $menulist = mysql_fetch_array($menu); while($menulist = mysql_fetch_array($menu)) { $themenu = $themenu . "<li><a href='#'>" . $menulist['link'] . "</a></li>"; } $echo $themenu; returns item 2 item 3 item 4 ... Any ideas why this might be Remove $menulist = mysql_fetch_array($menu); This extraneous call is pulling the first record, but