while-loop

Display text once within while loop on the first loop

拥有回忆 提交于 2019-12-02 21:25:19
<?php $i = 0; while(conditionals...) { if($i == 0) print "<p>Show this once</p>"; print "<p>display everytime</p>"; $i++; } ?> Would this only show "Show this once" the first time and only that time, and show the "display everytime" as long as the while loop goes thru? Yes, indeed. You can also combine the if and the increment, so you won't forget to increment: if (!$i++) echo "Show once."; Rather than incrementing it every time the loop runs and wasting useless resource, what you can do is, if the value is 0 for the first time, then print the statement and make the value of the variable as

Perl - while (<>) file handling [duplicate]

大憨熊 提交于 2019-12-02 20:38:25
This question already has an answer here : Which file is Perl's diamond operator (null file handle) currently reading from? (1 answer) A simple program with while( <> ) handles files given as arguments ( ./program 1.file 2.file 3.file ) and standard input of Unix systems. I think it concatenates them together in one file and work is line by line. The problem is, how do I know that I'm working with the first file? And then with the second one. For a simple example, I want to print the file's content in one line. while( <> ){ print "\n" if (it's the second file already); print $_; } The diamond

While loops, comparing more than one value

倖福魔咒の 提交于 2019-12-02 20:10:17
问题 I can understand how to use the while loop perfectly if just to compare it with one thing, for example: x=int(input("Guess my number 1-10")) while x!=7: print("Wrong!") x=int(input("Try again: ")) print("Correct it is 7. ") However, if I want to compare two or more values through while loops (especially if I want to validate something), I would do something like this: number=input("Would you like to eat 1. cake 2. chocolate 3. sweets: ") while number!= "1" or number != "2" or number != "3":

Variable issues in SSH

拈花ヽ惹草 提交于 2019-12-02 19:57:36
问题 Hey guys I'm trying to run this code: #!/bin/bash sudo /usr/local/bin/sshpass -p pwd ssh -o stricthostkeychecking=no -p 11022 admin@$1.test.com<<EOI i=1 while read line do location="sudo sed -n ${i}p /Users/Shared/$1.txt" number="sudo sed -n ${i}p /Users/Shared/$2n.txt" my_array=("${my_array[i]}" $line) sudo cp /Applications/TEPS\ OS\ X\ Share\ Folder/MAIN\ IMAGES\ FOLDER\ ƒ/${location}${number} /Users/Shared/FYP/$number sudo sips -Z 256 /Users/Shared/FYP/$number /Users/Shared/FYP/$number ((i

How to precalculate valid number of combinations instead of using while loop?

时光怂恿深爱的人放手 提交于 2019-12-02 19:47:40
问题 Given List of datacenters which are dc1, dc2, dc3 and list of machines, h1, h2, h3, h4 as mentioned below - Datacenters = dc1, dc2, dc3 Machines = h1, h2, h3, h4 I want to generate below combinations only - a) {dc1=h1, dc3=h3, dc2=h2} b) {dc1=h2, dc3=h4, dc2=h3} c) {dc1=h3, dc3=h1, dc2=h4} d) {dc1=h4, dc3=h2, dc2=h1} Each datacenter in the each pass should get alternate machines/hosts. They should not get same machines. For example in a as shown above - dc1 gets h1 , dc2 gets h2 , dc3 gets h3

exiting a while loop with a negative integer

橙三吉。 提交于 2019-12-02 19:42:58
问题 I want to exit a while() when the user enters a negative number of any size. What kind of condition would I need at the start of the loop to get the loop to exit when the user enters a negative number? 回答1: Use an if condition to know the number is less than 0 or not. And if yes, just use break statement inside it, which will bring you out of the loop. Learn more about break statement from MSDN. 回答2: Well, what is a negative number? It's a number (call it x ) that is less than zero, or

Assigning value in while loop condition

扶醉桌前 提交于 2019-12-02 19:33:32
I found this piece of code on Wikipedia. #include <stdio.h> int main(void) { int c; while (c = getchar(), c != EOF && c != 'x') { switch (c) { case '\n': case '\r': printf ("Newline\n"); break; default: printf ("%c",c); } } return 0; } I'm curious about expression used as condition for while loop: while (c = getchar(), c != EOF && c != 'x') It's quite obvious what it does, but I've never seen this construction before. Is this specific to while loop? If not, how does parser/compiler determine which side of comma-separated expression returns boolean value for while loop? The comma operator is a

while loop not ending when required

房东的猫 提交于 2019-12-02 18:46:41
问题 So some background information, I'm new to programming and am still learning, so I apologize for my trivial error making. I am making my own text based game just to further practice etc. Here is the link to everything on dropbox for more context: https://www.dropbox.com/sh/uxy7vafzt3fwikf/B-FQ3VXfsR I am currently trying to implement the combat system for my game, and I am running into the issue of the combat sequence not ending when required. The 'combat sequence' is a while loop as follows:

c# loop until Console.ReadLine = 'y' or 'n'

我与影子孤独终老i 提交于 2019-12-02 18:46:19
问题 I'm fairly new to c#, and writing a simple console app as practice. I want the application to ask a question, and only progress to the next piece of code when the user input equals 'y' or 'n'. Here's what I have so far. static void Main(string[] args) { string userInput; do { Console.WriteLine("Type something: "); userInput = Console.ReadLine(); } while (string.IsNullOrEmpty(userInput)); Console.WriteLine("You typed " + userInput); Console.ReadLine(); string wantCount; do { Console.WriteLine(

Python: While, if, else counting

淺唱寂寞╮ 提交于 2019-12-02 18:24:33
问题 I'm having a bit of an issue with a while/if statement. I have a list of values, normally these values will be strings, but sometimes it can return None. Here are two of my attempts: x = ['One','Two','Three',None,None] New = [] count=0 for y in x: while isinstance(y,str): New.append(y) count+=1 break else: count+=1 New.append('New - '+str(count)) print New,count >>> The list repeats several times New = [] for y in x: count=0 if y is not None: New.append(y) count+=1 else: count+=1 New.append(