如果你在perl中有一个简单的正则表达式替换,如下所示:
($line =~ s/JAM/AAA/g){
Run Code Online (Sandbox Code Playgroud)
我将如何修改它以便它查看匹配并使替换与匹配的情况相同,例如:
'JAM'将成为'AAA','jam'将成为'aaa'
你会如何在perl中执行以下操作:
for $line (@lines) {
if ($line =~ m/ImportantLineNotToBeChanged/){
#break out of the for loop, move onto the next line of the file being processed
#start the loop again
}
if ($line =~ s/SUMMER/WINTER/g){
print ".";
}
}
Run Code Online (Sandbox Code Playgroud)
更新以显示更多代码,这是我正在尝试做的事情:
sub ChangeSeason(){
if (-f and /.log?/) {
$file = $_;
open FILE, $file;
@lines = <FILE>;
close FILE;
for $line (@lines) {
if ($line =~ m/'?Don't touch this line'?/) {
last;
}
if ($line =~ m/'?Or this line'?/){
last;
}
if …Run Code Online (Sandbox Code Playgroud) 来源提供商 - 列出分支机构:
谁能告诉我“providerName”字符串的用途是什么?
如果我有一个包含目录(包含冒号)的.properties文件:
some_dir=f:\some\dir\etc
another_dir=d:\dir\some\bin
Run Code Online (Sandbox Code Playgroud)
然后使用ConvertFrom-StringData将Key = Value对从所述属性文件转换为哈希表:
$props_file = Get-Content "F:\dir\etc\props.properties"
$props = ConvertFrom-StringData ($props_file)
$the_dir = $props.'some_dir'
Write-Host $the_dir
Run Code Online (Sandbox Code Playgroud)
Powershell抛出一个错误(不喜欢冒号):
ConvertFrom-StringData : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'StringData'. Specified method is not supported.
At line:3 char:32
+ $props = ConvertFrom-StringData <<<< ($props_file)
+ CategoryInfo : InvalidArgument: (:) [ConvertFrom-StringData], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand
Run Code Online (Sandbox Code Playgroud)
你怎么绕这个?我希望能够使用它来引用目录.符号:
$props.'some_dir'
Run Code Online (Sandbox Code Playgroud) 使用以下powershell脚本:
Get-ChildItem hkcu:\Test\Universe\Datawink\Switch1\Devices | ForEach-Object {Get-ItemProperty $_.pspath}
Run Code Online (Sandbox Code Playgroud)
我能够得到以下输出:
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices\Entry1
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices
PSChildName : Entry1
PSProvider : Microsoft.PowerShell.Core\Registry
Device : 2882881001
Recordable : Yes
Active : Yes
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices\Entry2
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices
PSChildName : Entry2
PSProvider : Microsoft.PowerShell.Core\Registry
Device : 2882881002
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices\Entry3
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Test\Universe\Datawink\Switch1\Devices
PSChildName : Entry3
PSProvider : Microsoft.PowerShell.Core\Registry
Device : 2882881003
Run Code Online (Sandbox Code Playgroud)
哪个好,它向我显示了我想要的信息,但我真正需要的是名为'Device'的字符串条目的值,所以我真正想要的是脚本的输出如下所示:
Device : 2882881001
Device : 2882881002
Device : 2882881003
Run Code Online (Sandbox Code Playgroud)
或理想情况下,这:
2882881001
2882881002
2882881003
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最简单方法是什么?
我有一个包含如下数据的文件:
2012-01-02 GREEN 4
2012-01-02 GREEN 6
2012-01-02 GREEN 7
2012-01-02 BLUE 4
2012-01-02 BLUE 3
2012-01-02 GREEN 4
2012-01-02 RED 4
2012-01-02 RED 8
2012-01-02 GREEN 4
2012-01-02 YELLOW 5
2012-01-02 YELLOW 2
Run Code Online (Sandbox Code Playgroud)
我不能总是预测第二列中的字符串是什么(所以在上面的例子中有颜色,但数据文件可以包含第二列中的任何字符串).但是在第三列中总是有一个数字(我希望第二列中的特定字符串的最大值).是不是能够:
对于每个唯一的字符串,获得最大的相关值(所以使用上面的内容,你最终得到以下内容)?:
2012-01-02 GREEN 7
2012-01-02 BLUE 4
2012-01-02 RED 8
2012-01-02 YELLOW 5
Run Code Online (Sandbox Code Playgroud)或者使用Perl(甚至是shell)会更容易吗?任何代码示例非常感谢!
我已经学习 c# 大约 3 个月了,今天我遇到的一件事是对象上的深拷贝和浅拷贝构造函数的概念(刚刚习惯了基类、继承和实例化的概念——多态性仍然没有)老实说,它真的沉没了......我离题了)。
当看到基类(在本例中为用户类)上的深度复制构造函数时,我的第一个想法是“你到底为什么要复制一个对象?”。我看过的文章解释了如何做以及它是如何工作的,但我仍然无法找到任何实际示例来说明为什么要这样做。如果我有一个 person 类,我肯定会创建一个 person 类的新实例吗?
我很欣赏这里可能缺少一些基本的东西,但如果有人能填补空白,那就太好了。一个真实的例子来证明它的有用性会更好!干杯!
请考虑以下代码.基本思想是返回一个逗号分隔的20个数字列表,其中任何一个数字都可以是介于0和最大允许值之间的值.在每次通过循环时,列表中下一个值允许的潜在最大值减少了所选择的最后一个随机值.
所以它有效...但它很糟糕.问题是它总是会产生这样的输出:
"22,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
"17,3,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
"23,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
如何修改以允许在整个范围内更均匀地分配值?
public static string RandomRangeOfValuesMaker(int total)
{
var remainingTotal = total + 1;
const int numberOfValues = 20;
var rand = new Random();
var builder = new StringBuilder();
for (var i = 1; i < numberOfValues + 1; i++)
{
var currentRandomNumber = rand.Next(remainingTotal);
remainingTotal = remainingTotal - currentRandomNumber;
builder.Append(currentRandomNumber + ",");
}
var rangeOfValues = builder.ToString().Remove(builder.ToString().Length - 1);
return rangeOfValues;
}
Run Code Online (Sandbox Code Playgroud)