假设您要编写一个处理多个文本文件的应用程序,在命令行中作为参数提供(例如,MyProcessor file1 file2 ...).这是一个非常常见的任务,经常使用Perl,但是如果想直接利用.NET并使用C#会怎样.
什么是最简单的C#4.0应用程序锅炉板代码,允许您这样做?它应该基本上包括逐行处理每个文件的每一行,并通过调用一个函数来处理它,或者可能有更好的方法来进行这种"组"行处理(例如,LINQ或其他一些方法).
.NET 4.0框架中的任何位置都存在以下通用函数吗?我想重复使用它,而不是自己编写它:
public static class Lambda
{
public static U Wrap<U>(Func<U> f)
{
return f();
}
}
Run Code Online (Sandbox Code Playgroud)
它允许以下构造(即嵌入在LINQ查询的select子句中的lambda表达式):
string test="12,23,34,23,12";
var res=from string s in test.Split(',')
select Lambda.Wrap(() => {string u=s+s; return int.Parse(u);});
Run Code Online (Sandbox Code Playgroud)
更新:对于质疑此解决方案的所有人,请查看Onkelborg的答案,了解如何在没有Lambda.Wrap()的情况下包含lambda(同时仍保持查询语法).请不要消除lambda.这必须适用于abitrary(值返回)lambdas.另外,我正在寻找一种查询语法解决方案.请不要将表达式转换为流畅的语法,从而轻视它.
可能重复:
在TSQL中转换整数并连接到varchar
如何使用具有更复杂值的命名参数调用存储过程.这是一个有点炮制的例子:
EXEC MyStoredProc @Param1='My name is: '+@Name
Run Code Online (Sandbox Code Playgroud)
要么:
EXEC MyStoredProc @Param1=CONCAT('My name is: ',@Name)
Run Code Online (Sandbox Code Playgroud)
尝试使用变量@Name连接文字字符串'My name is:'时出错.圆括号没有任何帮助.这是T-SQL的限制(即,当使用命名参数时,等号后的表达式必须是单个文字或变量)?
谢谢
我想将reg-ex的单个捕获作为标量传递给子程序,我该怎样做呢?这是一个例子:
sub myfunc($)
{
my ($value)=@_;
# Do something with $value...
}
# This is the data we want to parse
my $some_var='value: 12345'; # For example
# We want to extract the value '12345' from $some_var
# and pass it to the myfunc subroutine as a scalar
# Attempt #1: This doesn't work
myfunc($some_var=~/value: (\d+)/);
# Attempt #2: This does work, but seems overly complicated
myfunc(join('',$some_var=~/value: (\d+)/));
Run Code Online (Sandbox Code Playgroud)
有没有比尝试#2更好的方法?
更新:
Oesor的回答完全给出了我想要避免调用的内容join:
myfunc(($some_var=~/value: (\d+)/)[0]);
Run Code Online (Sandbox Code Playgroud) 假设我们有一个字符范围:
// For example
const char *valueBegin="123blahblah"; // Beginning of value
const char *valueEnd=valueBegin+3; // One past end of value
Run Code Online (Sandbox Code Playgroud)
...,我想将其转换为int:
int value=...// Given valueBegin and valueEnd, calculate
// the number stored starting at valueBegin
Run Code Online (Sandbox Code Playgroud)
有什么好的C++ 11方法可以做到这一点?
显然,您可以创建std::string并使用stoi或将其复制到临时的NUL终止字符数组,然后它很容易(例如,通过atoi或strtol).
想想一种不涉及将字符复制到某个临时数组/对象的方法 - 换句话说就是一个就地处理字符数据的函数.
更新:
很多答案,但请在回答之前考虑一下.范围没有NUL终止,因此需要valueEnd.你不知道超出值的是什么(也就是说,valueEnd可能超出了包含该值的缓冲区),所以如果你的答案没有使用valueEnd,那就错了.此外,如果您的答案创建了一个临时std::string对象,则不在此问题的指导范围内.
给定以下应用程序代码read-data,该代码仅将数据读取stdin到堆上分配的缓冲区中buf:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const size_t BUF_SIZE=1048576*256; // Just for testing, don't do this in prod code
const size_t MAX_READ_SIZE=1048576;
int main(int argc, char *argv[])
{
// Allocate buffer space on the heap
char *buf=(char *) malloc(BUF_SIZE);
// Check for malloc failure
if (buf==NULL)
{
fprintf(stderr,"Unable to allocate %zu bytes\n");
return 1;
}
size_t curOffset=0;
// Read MAX_READ_SIZE (or smaller) blocks until EOF
// WARNING: Don't do this in actual "live" …Run Code Online (Sandbox Code Playgroud) 给定以下代码,期望每次select()调用时都有一秒钟的睡眠.但是,睡眠仅在第一次呼叫时发生,所有后续呼叫都不会导致延迟:
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
for (;;)
{
/* Sleep for one second */
int result=select(0, NULL, NULL, NULL, &tv);
printf("select returned: %d\n",result);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么所有的电话select()除了第一次立即返回?
编译器:gcc 4.9.2
操作系统:Centos 7(Linux)
内核信息:3.10.0-327.36.3.el7.x86_64