Powershell将阵列与get-random结合起来

时间:2018-05-24 09:55:17

标签: powershell

你好以下是我的瞬间代码..

从列表中抽取7个随机“餐”,然后在几天内将它们命令到每周列表中。

# Food selector for the week!
#random Stuff mixed for every day.

Enum Food
{#Add Food here:
    Tacos
    Pizza
    Quesedias
    Lasagne
    Älplermakkaronen
    Apfelwähe
    Apprikosenwähe
    Rabarberwähe
    Käsekuchen
    Pasta
    Ravioli
    Empanadas
    Hamburger

}
function Food {
    $foodsOfWeek = [Enum]::GetValues([Food]) | Get-Random -Count 7

    foreach ($day in [Enum]::GetValues([DayOfWeek])) {
        ([string]$day).Substring(0, 3) + ': ' + $foodsOfWeek[[DayOfWeek]::$day]
    }

}

我正在努力使它可以与更多这样的数组组合:

Enum Food
{#Add Food here:
    Tacos
    Pizza
    Quesedias
    Lasagne
    Älplermakkaronen
    Apfelwähe
    Apprikosenwähe
    Rabarberwähe
    Käsekuchen
    Pasta
    Ravioli
    Empanadas
    Hamburger
}
Enum Food2
{#Add Fish Stuff here:
    Whatever Fish I want^^ :)
}

#and an array for meat(like steak)
.....

#an array for som healthy food!
.....



function Food {
    $foodsOfWeek = [Enum]::GetValues([Food]) | Get-Random -Count 7

    foreach ($day in [Enum]::GetValues([DayOfWeek])) {
        ([string]$day).Substring(0, 3) + ': ' + $foodsOfWeek[[DayOfWeek]::$day]
    }

}

所以它确实将它们组合在一起并将RANDOM全部用于它们,但我可以设置标准,就好像它必须至少从每个“List”中都有一个。 完美将是: 每周至少一次 - >肉,鱼,蔬菜,然后其余的是从第一个列表中随机...

我希望你们能帮助我:)。

亲切的问候亚历克斯

1 个答案:

答案 0 :(得分:0)

虽然这可能不是您想要的,但您可以尝试以下方法:

{
# Food selector for the week!
#random Stuff mixed for every day.

Enum FastFood
{#Add Food here:
Tacos
Pizza
Quesedias
Lasagne
Älplermakkaronen
Apfelwähe
Apprikosenwähe
Rabarberwähe
Käsekuchen
Pasta
Ravioli
Empanadas
Hamburger

}

Enum Meat
{#Add Food here:
Steak
Chop
Beaf
Lamb
Pork
Chicken
}

function Food {
#either
  $Foods =  [Enum]::GetValues([FastFood]) + [Enum]::GetValues([Meat])
#or
  $Foods =  [Enum]::GetValues([FastFood])
  $Foods += [Enum]::GetValues([Meat])

$foodsOfWeek = $Foods | Get-Random -Count 7

foreach ($day in [Enum]::GetValues([DayOfWeek])) {
    ([string]$day).Substring(0, 3) + ': ' + $foodsOfWeek[[DayOfWeek]::$day]
}

$ Foods变量当然不是Enum类型,而是一个对象集合,但是你可以随后生成随机的餐点。当天和可以选择在添加其他类别时扩展列表。要访问特定条目,您可以按如下方式编制索引:$ Foods [10]

当前变量包含19个元素($ Foods.count)

希望它有所帮助,