好像
while( condition ) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
完全等同于
for( ; condition; ) {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
有没有理由使用后者而不是前者?
我正在学习Perl并注意到一个相当特殊的怪癖 - 尝试在while循环中匹配多个正则表达式条件之一导致该循环继续进行无穷大:
#!/usr/bin/perl
my $hivar = "this or that";
while ($hivar =~ m/this/ig || $hivar =~ m/that/ig) {
print "$&\n";
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
this
that
that
that
that
[...]
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会这样?有没有比这更笨拙的变通办法:
#!/usr/bin/perl
my $hivar = "this or that";
while ($hivar =~ m/this|that/ig) {
print "$&\n";
}
Run Code Online (Sandbox Code Playgroud)
这是我遇到的现实世界问题的简化,虽然我从实际角度对此感兴趣,但我也想知道幕后是什么触发了这种行为.这个问题似乎与Google不兼容.
谢谢!
汤姆
我有一个以块的形式读取文件的函数.
public static DataObject ReadNextFile(){ ...}
Run Code Online (Sandbox Code Playgroud)
dataobject看起来像这样:
public DataObject
{
public string Category { get; set; }
// And other members ...
}
Run Code Online (Sandbox Code Playgroud)
基本上我想做的是以下内容
List<DataObject> dataObjects = new List<DataObject>();
while(ReadNextFile().Category == "category")
{
dataObjects.Add(^^^^^ the thingy in the while);
}
Run Code Online (Sandbox Code Playgroud)
我知道它可能不是这样做的,因为我如何访问我刚读过的对象.
我目前正试图弄清楚如何迭代JSON响应中的所有对象.我的对象可能有无限的子对象,也可能有无限的子对象.
{
"obj1" : {
"obj1.1" : "test",
"obj1.2" : {
"obj1.1.1" : true,
"obj1.1.2" : "test2",
"obj1.1.3" : {
... // etc
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道是否有一个开箱即用的脚本可以处理这种类型的对象?
这是我的foobar.sh的代码:
!#/bin/bash
while [ 1 ]
do
pid=`ps -ef | grep "mylittleprogram" | grep -v grep | awk ' {print $2}'`
echo $pid
if [ "$pid"="" ]
then
echo "Process has ended lets get this show on the road..."
exit
else
echo "Process has not ended yet"
fi
sleep 6
done
Run Code Online (Sandbox Code Playgroud)
我基本上运行一个infinate循环,一旦受监视的进程结束就会执行命令X但是当我的脚本循环时我最终收到以下消息:
./foobar.sh: line 7: [: missing `]'
Process has not ended yet
Run Code Online (Sandbox Code Playgroud)
有没有办法让脚本接受零反馈将触发我的'Then'语句并执行命令X,因为它不喜欢当前的方法.
我有一个while循环,它只是一个方法调用.我在循环的外部有一个计时器和另一个计时器,它会逐渐增加方法调用在循环内部的时间.外部时间约为17秒,内部计时器的总时间为40毫秒.循环执行50,000次.以下是代码示例:
long InnerTime = 0;
long OutterTime = 0;
Stopw1.Start();
int count = 1;
while (count <= TestCollection.Count) {
Stopw2.Start();
Medthod1();
Stopw2.Stop();
InnerTime = InnerTime + Stopw2.ElapsedMilliseconds;
Stopw2.Reset();
count++;
}
Stopw1.Stop();
OutterTime = Stopw1.ElapsedMilliseconds;
Stopw1.Reset();
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.马西莫
我正在做一个简单的shell.它还需要能够按行读取文本文件.这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
// Exit when called, with messages
void my_exit() {
printf("Bye!\n");
exit(0);
}
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
// Char array to store the input
char buff[1024];
// For the fork
int fid;
// Get all the environment variables
char dir[50];
getcwd(dir,50);
char *user = getenv("USER");
char *host = getenv("HOST");
// Issue the prompt here.
printf("%s@%s:%s> ", user, host, dir);
// If …Run Code Online (Sandbox Code Playgroud) 这种观察并不重要,因为浪费在循环语句上的时间性能可能远高于循环本身.但无论如何,我会分享它,因为我搜索并找不到关于此的话题.我总是有这样的印象:预先分配我将循环的数组,然后循环它,将比直接循环它更好,并决定检查它.代码将比较这两个fors之间的效率:
disp('Pure for with column on statement:')
tic
for k=1:N
end
toc
disp('Pure for with column declared before statement:')
tic
m=1:N;
for k=m
end
toc
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是:
Pure for with column on statement:
Elapsed time is 0.003309 seconds.
Pure for with column declared before statement:
Elapsed time is 0.208744 seconds.
Run Code Online (Sandbox Code Playgroud)
为什么会这样?不应该预先分配更快?
事实上,matlab help for说:
因为从不创建索引向量,所以当结束表达式出现在FOR语句中时,长循环可以提高内存效率.
因此,与我的期望相反,for语句中的列表达式更好,因为它不分配向量,因此更快.
我做了以下脚本来测试其他场合我也认为会更快:
% For comparison:
N=1000000;
disp('Pure for loop on cell declared on statement:')
tic
for k=repmat({1},1,N)
end
toc
disp('Pure for loop on …Run Code Online (Sandbox Code Playgroud) 我正在看一个用经典ASP编写的网页,我正处于'while'语句的中间.我想使用Chrome开发者工具调试页面上的值.我试过插入
console.log(value);
Run Code Online (Sandbox Code Playgroud)
它适用于C#应用程序,但它Expected end of statement在console.log我插入的行中抛出了一个.
我只是在寻找关于如何使用经典asp调试器的快速提示?
如何在此代码中使用while循环每2秒读取一次串行并在Label中显示?这个应用程序将在运行中挂起,我是python的新手来解决这个问题.
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from time import sleep
import serial
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.rows = 2
ser = serial.Serial('COM3', 9600, timeout=0)
while 1:
sleep(2)
ser.read()
data = ser.read()
self.add_widget(Label(text=str(data)))
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
Run Code Online (Sandbox Code Playgroud) while-loop ×10
loops ×3
c# ×2
for-loop ×2
asp-classic ×1
bash ×1
c ×1
c++ ×1
console.log ×1
debugging ×1
each ×1
fgets ×1
if-statement ×1
jquery ×1
json ×1
kivy ×1
matlab ×1
performance ×1
perl ×1
pycharm ×1
python ×1
regex ×1
shell ×1