我正在尝试编写一个java程序,该程序在Java中使用Strings并在perl脚本中替换相应的文本序列.这是我的代码:
String sedFirstLine = "'s/AAA/"+newFirstLine+"/'";
String sedNewCntr = "'s/BBB/"+newCntr+"/'";
String sedNewSpacing = "'s/SPACE/"+newSpacing+"/'";
String sedNewDmax = "'s/MAX/"+newDmax+"/'";
String sedInputFile = "/filepath/myPerlScript.pl"
String sedOutputFile = "/filepath/myNewPerlScript.pl";
String[] cmdArray3 = {"sed", "-e", sedFirstLine,"-e", sedNewCntr,"-e", sedNewSpacing,"-e", sedNewDmax, "-e", sedInputFile, ">", sedOutputFile};
Process runCmd;
runCmd = Runtime.getRuntime().exec(cmdArray3);
Run Code Online (Sandbox Code Playgroud)
当我运行此程序时,不会生成输出文件"myNewPerlScript.pl".我不确定我写的是什么问题.我之前提到的Java变量是"newFirstLine","newCntr"等.
因此,为了打印从1到10的数字,我们编写一个简单的for循环,从i = 1到i <= 10,并期望看到数字1 2 3 .. 10打印出来.我很好奇如果我为这样的条件添加额外的零会发生什么:
for(int i=000000; i<000010; i++){
System.out.println(i)
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是
0
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)
为什么要打印这些数字?
我正在VS中运行一个c ++程序.我提供了一个正则表达式,我正在解析一个超过200万行的文件,用于匹配该正则表达式的字符串.这是代码:
int main() {
ifstream myfile("file.log");
if (myfile.is_open())
{
int order_count = 0;
regex pat(R"(.*(SOME)(\s)*(TEXT).*)");
for (string line; getline(myfile, line);)
{
smatch matches;
if (regex_search(line, matches, pat)) {
order_count++;
}
}
myfile.close();
cout << order_count;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该文件应搜索匹配的字符串并计算它们的出现次数.我有一个python版本的程序,使用相同的正则表达式在4秒内完成.我已经等了大约5分钟才能使上面的c ++代码工作,但还没有完成.它没有遇到无限循环,因为我让它以一定的间隔打印出当前行号并且它正在进行中.我应该用不同的方式编写上面的代码吗?
编辑:这是在发布模式下运行.
编辑:这是python代码:
class PythonLogParser:
def __init__(self, filename):
self.filename = filename
def open_file(self):
f = open(self.filename)
return f
def count_stuff(self):
f = self.open_file()
order_pattern = re.compile(r'(.*(SOME)(\s)*(TEXT).*)')
order_count = 0
for line in f:
if order_pattern.match(line) != None:
order_count+=1 # …Run Code Online (Sandbox Code Playgroud) 在此问题立即被标记为重复之前,让我说我已经尝试了每个 解决方案 ,这是与我的情况最相关的两个问题.如果有人能在必要时关闭此问题之前至少查看我的特定问题,我将不胜感激.
我有一个名为e的有限状态机对象,它是一个MCFiniteSM对象.e的核心是名为state_dict的字典,其存储"进程"id('1','2'等)以及存储关于每个"进程"的更多信息的相关字典.我正在运行unittests来添加进程,根据给定的参数更改状态等.但是,在unittest文件中的函数调用之间,有限状态机似乎被清除了.我已经查看了上面列出的两个问题,以避免这种情况并坚持更改,但无论我尝试对有限状态机的更改都不会持久化.这是最简单的文件.
from finite_state_machine import MCFiniteSM
from unittest import TestLoader, TestCase, main as unimain
from datetime import datetime
import time, calendar
class MyUnitTest(TestCase):
@classmethod
def setUpClass(cls):
cls.e = MCFiniteSM()
cls.timestamp = datetime.strftime(datetime.fromtimestamp(calendar.timegm(time.gmtime())), '%Y/%m/%d %H:%M:%S')
class TestFSM(MyUnitTest):
@classmethod
def setUpClass(cls):
super(TestFSM, cls).setUpClass()
#e = MCFiniteSM()
#timestamp = datetime.strftime(datetime.fromtimestamp(calendar.timegm(time.gmtime())), '%Y/%m/%d %H:%M:%S')
def test_add_convert_processes(self):
self.e.add_process('1', 'S', self.timestamp, 100, 'start message for process 1')
self.e.add_process('2', 'S', self.timestamp, 200, 'start message for process 2')
self.e.add_process('3', 'S', self.timestamp, 300, 'start message …Run Code Online (Sandbox Code Playgroud) 我正在尝试在不同的片段中重用 Dialog。我的应用程序的结构是一个在多个片段之间转换的活动。
我在 DialogFragment 中定义了一个侦听器接口,然后在我想在其中使用它的片段中实现了该接口。这是不同的片段在积极点击方面显示不同的行为。这是对话框片段
public class AreyouSureDialog extends DialogFragment {
public ConfirmationListener listener;
public interface ConfirmationListener {
public void onPositiveClick(DialogFragment dialog);
public void onNegativeClick(DialogFragment dialog);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.confirmation_dialog, null))
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onPositiveClick(AreyouSureDialog.this);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onNegativeClick(AreyouSureDialog.this);
}
});
// Create the AlertDialog object and return …Run Code Online (Sandbox Code Playgroud) 我有一个String [],我想传递给一个进程.由于一些奇怪的原因,我的IDE给了我一个错误,即支持预期,我的声明不是一个完整的声明.我已经检查了我的代码中的任何地方,我没有看到一个支架不合适,所以我很困惑为什么我得到这个错误.这是代码:
String[] cmdArray4 = {"/bin/tcsh","-c","sed -e 's/SPACE/"+matlab1spacing+"/' -e 's/MAX/"+matlab1dmax+"/' -e 's/NAME/"+matlab1filename"/' /filepath/matlabscript1.m > /filepath/matlabscript2.m"};
Process passVarstoMatlab;
passVarstoMatlab = Runtime.getRuntime().exec(cmdArray4);
Run Code Online (Sandbox Code Playgroud)
我试图逃避单引号字符,看看是否可能是问题,但这没有什么区别.更令人困惑的是,我在一个不同的方法中有一个非常相似格式的String [],它完全正常.这是String [],我用它作为参考:
String[] cmdArray3 = {"/bin/tcsh","-c", "sed -e 's/AAA/"+newFirstLine+"/' -e 's/BBB/"+newCntr+"/' -e 's/SPACE/"+newSpacing+"/' -e 's/MAX/"+newDmax+"/' /filepath/gnom_Dmax_scan.pl > /filepath/g2.pl"};
Run Code Online (Sandbox Code Playgroud)
也许有一些我没有看到的小错误,所以我很感激任何帮助.谢谢.
我有一个函数,递归搜索2d矩阵,找到值0并返回其位置.这是代码:
def findNextZero(x, y, board):
if board[x][y] == 0:
return (x, y)
else:
if y == (SIZE-1):
# if its at the edge of the "board", the 2d matrix
findNextZero(x+1, 0, board)
else:
findNextZero(x, y+1, board)
Run Code Online (Sandbox Code Playgroud)
当我打印(x,y)时,该函数将打印正确的元组.但是,如果我尝试返回它,则表示返回值为None.为什么会这样?
我正在尝试编写一个测试程序来检查用户对数据库的响应(基本上是3个数组).这是相关代码:
while(true) {
int r = randomnumbergenerator.nextInt(length);
String[] split2 = a[r].split(" ");
String lastNameAnswer = "";
while(!lastNameAnswer.equals(split2[1])) {
System.out.println("What is " + split2[0] + "\'s last name?");
lastNameAnswer = reader.next();
}
String hometownAnswer = "";
String hometown = h[r];
while(!hometownAnswer.equals(hometown)) {
System.out.println("What is " + split2[0] + "\'s hometown?");
hometownAnswer = reader.next();
}
String majorAnswer = "";
String major = m[r];
while(!majorAnswer.equals(major)) {
System.out.println("What is " + split2[0] + "\'s major?");
majorAnswer = reader.next();
}
}
Run Code Online (Sandbox Code Playgroud)
那么应该发生的是它应该从数据库中选择一个名称,并询问有关名称的问题,例如姓氏,家乡和专业.如果用户的答案错误,则应重复询问该问题.我的代码适用于姓氏和专业,但由于某种原因,故乡是故障.这是输出:
What is Ellen's …Run Code Online (Sandbox Code Playgroud) 我正在使用python 2.7
indent expected如果我写这样的函数,我的ide会显示
def foo():
#
Run Code Online (Sandbox Code Playgroud)
但如果我写这个就不行
def foo():
'''
'''
Run Code Online (Sandbox Code Playgroud)
这有什么原因吗?