旋转变量中的文本行

rotate lines of text in a variable

我有一个带有序列号行的字符串变量,我需要旋转变量中的序列号,然后将其输出到屏幕。 基本上把第一个连载移到最后,从头删掉。 我可以将第一个序列移到变量的末尾,但无法弄清楚如何从开头删除它。

例子

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
#append first serial number to the end
$serials +="`n$($serials.split()[0])" 
#remove the first serial from beginning.????

最终结果应该是:

"6542185`n6546848`n6654898`n6522828`n6546544"

如果您愿意使用集合,队列对象本身就具有以下功能:

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
# Create queue object with entries for each serial
$queue = [System.Collections.Queue]::new(-split $serials)

# Dequeues the top item and enqueues the item to the bottom
$queue.Enqueue($queue.Dequeue())

# Create string joined by newline character
$serials = $queue -join "`n"

# Optionally output a list and skip the string join above
$queue

这会起作用

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
$test = @()
$test = $serials.split()
$final = @()

for($i = 1; $i -lt  $test.count; $i++){

    $final += $test[$i]

}

$final += $test[0]
$final

从我的评论中扩展这个想法(不需要拆分)。

示例:

Clear-Host
Write-Verbose -Message "Current array" -Verbose
($serials = '6546544','6542185', '6546848', '6654898', '6522828')
"`n"
#append first serial number to the end
Write-Verbose -Message "Modified array" -Verbose
($serials += $($serials[0]))
"`n"
Write-Verbose -Message "Final array" -Verbose
($serials = ($serials | Select -Skip 1))
# Results
<#
VERBOSE: Current array
6546544
6542185
6546848
6654898
6522828


VERBOSE: Modified array
6546544
6542185
6546848
6654898
6522828
6546544


VERBOSE: Final array
6542185
6546848
6654898
6522828
6546544
#>

..然后以任何你希望的方式加入。

另一种方法是将连续剧拆分成一个数组,然后将该数组与附加到末尾的第一个元素重新组合:

# split on "`n" to get an array
$serials = "6546544`n6542185`n6546848`n6654898`n6522828" -split "`n"
# recombine the array in different order with "`n"
$serials = ($serials[1..($serials.Count - 1)] + $serials[0]) -join "`n"

$serials 现在包含 "6542185`n6546848`n6654898`n6522828`n6546544"

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
#append first serial number to the end
$first = ($serials.Split()[0]).trim()
$serials = ($serials -replace $first).trim()
$serials += "`n$($first)"