查找基于字符串列表第一个可用的序列

查找基于字符串列表第一个可用的序列

本文介绍了查找基于字符串列表第一个可用的序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串,如列表: apple01 apple02 apple04 banana02 cherry01 ,你会怎么想出的第一个的可用序列号的每个的类型 - 也就是 apple03 如果我问关于苹果 banana01 如果我问关于香蕉 cherry02 如果我问关于樱桃

我任务是自动化的Azure的虚拟机的创建,以及这些字符串实际上是主机现有的虚拟机的名称,由Azure的PowerShell命令报(GET-AzureRmResourceGroupDeployment -ResourceGroupName$ azureResGrpName2) .DeploymentName (或任何类似的有效)。

更新:这是我的工作code:

  $ rgdNames =(GET-AzureRmResourceGroupDeployment -ResourceGroupName$ azureResGrpName)。DeploymentName
$姐弟= $ rgdNames |位置对象{$ _ -match^($主机名)(\\ d +)$}
如果($兄弟姐妹){
    #使在主机名列表中的所有号码的列表
    $连续= @()
    的foreach($在$同胞兄弟姐妹){
        #$兄弟-split($同级-split'\\ D + $')<从拆分结束所有的数字,然后在正面去掉一切
        #然后将其转换为数字,并添加到$连续
        $ hostnumber = [转换] :: ToInt32([字符串] $($同胞-split($同级-split'\\ D + $'))[1],10)
        $连续+ = $ hostnumber
    }
    的foreach($ i的1 .. $ siblingsMax){#遍历所有有效的序列号
        如果(!$ serials.Contains($ I)){#停止的时候,我们发现一个序列号,是不是在现有的主机列表
                $串行= $我
                打破
            }
    }
}其他{
    $串行= 1
}


解决方案

这似乎这样的伎俩!

  ##创建变量来保存数
$ spareNumber = $空
##我们认为它们已经被分隔成组presuming
$苹果= @(apples01,apples002,apples3,apples15)
##创建一个空数组
$ NUM = @()
##你知道什么是的foreach是正确的? ;)
的foreach($在$苹果苹果)
{
    ##辛勤工作的一部分
    ## [转换] :: toint32转换,你猜对了......(并把它添加到$ NUM)
    ## $苹果-split($苹果-split'\\ D + $')<从拆分结束所有的数字,然后在正面去掉一切
    $ NUM + = [转换] :: ToInt32([字符串] $($苹果-split($苹果-split'\\ D + $'))[1],10)
}
##计数从1到10,并传递给FOREACH
(1..10)| {的foreach
    ##'当我们找到一个不在$ NUM,将其存储在sparenumber,打破了这个关节。
    如果(!$ num.Contains($ _)){
        $ spareNumber = $ _
        打破
    }
 }
 ##,这里是你的号码?
 $ spareNumber

Given a list of strings such as: apple01, apple02, and apple04, banana02, cherry01, how would you come up with the first available serial number of each type -- that is, apple03 if I ask about apple, or banana01 if I ask about banana, and cherry02 if I ask about cherry?

I'm tasked with automating the creation of Azure VM's, and these strings are actually host names of existing VM's, as reported by the Azure Powershell command (Get-AzureRmResourceGroupDeployment -ResourceGroupName "$azureResGrpName2").DeploymentName (or anything effectively similar).

Update: Here's my working code:

$rgdNames = (Get-AzureRmResourceGroupDeployment -ResourceGroupName "$azureResGrpName").DeploymentName
$siblings = $rgdNames | Where-Object{$_ -match "^($hostname)(\d+)$" }
if ($siblings) {
    # Make a list of all numbers in the list of hostnames
    $serials = @()
    foreach ($sibling in $siblings) {
        # $sibling -split ($sibling -split '\d+$') < split all digits from end, then strip off everything at the front
        # Then convert it to a number and add that to $serials
        $hostnumber = [convert]::ToInt32([string]$($sibling -split ($sibling -split '\d+$'))[1], 10)
        $serials += $hostnumber
    }
    foreach ($i in 1..$siblingsMax){ # Iterate over all valid serial numbers
        if (!$serials.Contains($i)) { # Stop when we find a serial number that isn't in the list of existing hosts
                $serial = $i
                break
            }
    }
} else {
    $serial = 1
}
解决方案

This seems to do the trick!

## Create variable to store number in
$spareNumber = $null
## we are presuming that they have already been seperated into groups
$apples = @("apples01","apples002","apples3","apples15")
## create an empty array
$num = @()
## You know what a foreach is right? ;)
foreach ($apple in $apples)
{
    ## the hard working part
    ## [convert]:: toint32 converts to, you guessed it... (and adds it to $num)
    ## $apple -split ($apple -split '\d+$') < split all digits from end, then strip off everything at the front
    $num += [convert]::ToInt32([string]$($apple -split ($apple -split '\d+$'))[1], 10)
}
## count from 1 to 10, and pass to foreach
(1..10) | foreach {
    ##'when we find one that isn't in $num, store it in sparenumber and break out of this joint.
    if (!$num.Contains($_)) {
        $spareNumber = $_
        break
    }
 }
 ## and here is your number...
 $spareNumber

这篇关于查找基于字符串列表第一个可用的序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:05