Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何将带分隔符的混合字符串列表转换为powershell脚本的数组_Arrays_String_Powershell - Fatal编程技术网

Arrays 如何将带分隔符的混合字符串列表转换为powershell脚本的数组

Arrays 如何将带分隔符的混合字符串列表转换为powershell脚本的数组,arrays,string,powershell,Arrays,String,Powershell,我想解决一个问题,我有一个逗号分隔的有序8位数字和范围(带前导零)的长文件,如下所示: 00001253,00001257-00001268,00001288,...,02154320,02154321,02154323-02154327,... 我想 (a) 将任何不在范围内的值作为令牌存储在PowerShell数组中,同时保留前导零 及 (b) 将范围扩展到所有相应的值,并将令牌存储在同一数组中。以下是迄今为止我为实现我的目标而编写的PowerShell“脚本”: $refids = @(

我想解决一个问题,我有一个逗号分隔的有序8位数字和范围(带前导零)的长文件,如下所示:

00001253,00001257-00001268,00001288,...,02154320,02154321,02154323-02154327,...
我想 (a) 将任何不在范围内的值作为令牌存储在PowerShell数组中,同时保留前导零 及 (b) 将范围扩展到所有相应的值,并将令牌存储在同一数组中。以下是迄今为止我为实现我的目标而编写的PowerShell“脚本”:

$refids = @(ARRAY_DERIVED_FROM_ABOVE_LIST)

foreach ($refid in $refids) {
New-Item c:\scripts\$refid.txt -type file -force -value "KEY:$refid"
}

关于如何进行有什么想法吗?提前感谢您的帮助

您可以从以下内容开始:

$string = '00001253,00001257-00001268,00001288,02154320,02154321,02154323-02154327'

$string.split(',') |
foreach {
  if ($_.Contains('-'))
    {
     invoke-expression ($_.replace('-','..')) |
     foreach {'{0:D8}' -f $_}
    }

  else {$_}
 }

00001253
00001257
00001258
00001259
00001260
00001261
00001262
00001263
00001264
00001265
00001266
00001267
00001268
00001288
02154320
02154321
02154323
02154324
02154325
02154326
02154327

乔利诺的回答很好。有些人不使用
调用表达式
。下面是另一个例子,它完成了相同的事情,同时展示了一种稍微不同的方法

$string = '00001253,00001257-00001268,00001288,02154320,02154321,02154323-02154327'

$string.split(',') | ForEach-Object {
    If($_.Contains('-')){
        $_.Split("-")[0]..$_.Split("-")[1]
    } Else {
        $_
    } 
} | ForEach-Object{ Write-Output ([string]$_).PadLeft(8,"0")}

嗨,马特-也感谢你的帮助-我用乔利诺的回答实施了一个令人满意的解决方案。是否有理由不使用“调用表达式”?您希望避免对用户生成的输入执行调用表达式(例如,通过导入csv文件或执行读取主机),因为用户可以输入powershell或随后将执行的命令代码。这似乎是机器生成的输入。感谢您的帮助-这似乎提供了一个可行的解决方案