while-loop

sed replace variables while read lines

♀尐吖头ヾ 提交于 2020-01-25 00:07:15
问题 I'm working on a shell script and i need to change some strings from different lines of a file into a while read statement. The structure need to be like this, because the String_Search and String_result will be calculated on each line. while read line do varA="String_Search" resA="String_Result" line=`echo $line | sed -e "s/$varA/$resA"` echo $line >> outputFile.txt done < "inputFile.txt" The script doesn't works and its showing to me this error message: sed: -e expression #1, char 31:

sed replace variables while read lines

微笑、不失礼 提交于 2020-01-25 00:06:44
问题 I'm working on a shell script and i need to change some strings from different lines of a file into a while read statement. The structure need to be like this, because the String_Search and String_result will be calculated on each line. while read line do varA="String_Search" resA="String_Result" line=`echo $line | sed -e "s/$varA/$resA"` echo $line >> outputFile.txt done < "inputFile.txt" The script doesn't works and its showing to me this error message: sed: -e expression #1, char 31:

scanf in while loop

岁酱吖の 提交于 2020-01-24 13:54:06
问题 In this code, scanf works only once. What am I doing wrong? #include <stdio.h> #include <unistd.h> int main() { int i = 1; if (! fork()) { while(i) { printf("Enter i"); scanf("%d", &i); fflush(stdin); fflush(stdout); } } else { printf("Parent\n"); } return(0); } 回答1: It has already been recommended that you not use scanf. If you feel you must use scanf , you really should be checking the return value to determine if an input error occurred prior to the conversion. It has also been noted that

What's the reason for this no-op while-loop used for assert macro?

孤街浪徒 提交于 2020-01-24 12:18:28
问题 I'm reviewing a codebase where assert macro is expanded like this in non-debug configurations: #define assert( what ) while( 0 )( ( void )1 ) which I don't quite get. Obviously the goal is to have a no-op. Then why not expand into an empty string? #define assert( what ) What's the reason for this no-op loop? 回答1: Most likely to avoid compiler warnings. Check whether this code provokes a warning about an empty statement: if (foo); If it does, then do you want the same warning in release mode

Variable scope in for-loop and while-loop [duplicate]

拜拜、爱过 提交于 2020-01-24 09:56:47
问题 This question already has answers here : php variable scope (6 answers) Closed 5 years ago . I'm new in PHP, I don't understand why the final result of the code below is '233' instead of '231', isn't the $a in foreach is a temp variable? <?php $a = '1'; $c = array('2', '3'); foreach($c as $a){ echo $a ; } echo $a; ?> Can anyone help? Thks. Updated 2014-11-28 Now I know what was my problem. As the accepted answer and this answer have pointed out, neither the foreach nor the while act like

Why is this code an infinite loop?

戏子无情 提交于 2020-01-23 23:59:58
问题 Before completing this code, I just tested it by mistake and realized that it will not stop: $var = "any"; for ($i=1; $i < 2; $i++){ $var.$i = "any"; } Why does this produce an infinite loop? And why doesn't PHP produce an error? 回答1: I did a simple test : echo $i; $var.$i = "any"; var_dump($var); Result : 1string(3) "any" anzstring(3) "any" So $i get transformed to "anz" and doesn't pass the validation to get out of the loop. $var.$i = "any"; is not really correct, i don't know what you are

Loop through an array MIPS Assembly

爷,独闯天下 提交于 2020-01-23 08:32:18
问题 I'm working on a program which loops through an array of 10 numbers. The first 9 elements have values higher than 0, the 10th has a value of 0. The loop should break when the 0 is encountered. i=0; while(A[i]!=0) { A[i]=A[i]+1; i++; } I know I can use 'beq' to break the loop if the value of the register is equal to 0. However I don't know enough about manipulating values in the memory. It's my first time using MIPS and you'll see it's a mess. If you can't fix it for me, can you give me some

Why can't I use an integer as a condition for a while loop in C#? [closed]

泄露秘密 提交于 2020-01-22 03:41:22
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I am getting an error using while loop in this manner, and I don't understand why: int count = 5; while(count--) //here showing an error { Console.WriteLine("Number : {0}", count); } However, in C, the code works properly. I am little bit confused about why I am getting this error. Can anyone explain why? 回答1: C

Can anyone tell me why these functions are not giving me a result in a reasonable spectrum?

て烟熏妆下的殇ゞ 提交于 2020-01-22 03:39:07
问题 The full code I am using is listed below, it is supposed to simulate a game of craps and print details to the user and allow for betting if the user desires it. Everything functions except for the actual craps game. Instead of looping only while there is not a truth value associated to crapsResult, it finds one real value and an incomprehensible string of a single negative number. Any help would be appreciated. int main() { //Declare the user input variables int gamesPlayed = 0; char

Python: How to end a while loop while it is running if the while condition changes during the loop?

喜夏-厌秋 提交于 2020-01-22 02:06:55
问题 I need some help with code in a text based game I am trying to make. My game uses health, and the code starts off with "while health>0:", and in another point in the game, when health eventually =0, the loop still continues. How do I make the loop end when health=0, without finishing the whole loop. Here is an example: health=100 while health>0: print("You got attacked") health=0 print("test") Should the code not be stopping when health=0, and not print "test"? How to I get it to stop when