简体   繁体   English

Powershell将阵列与get-random结合

[英]Powershell Combine Arrays with get-random

Hello Below is my momentary Code.. 您好,下面是我的瞬时代码。

It takes seven random „meals“ out of an list and then orders them into a weekly list ordert in days. 它从列表中抽取了七个随机的“餐点”,然后在几天之内将它们订购到每周的列表中。

# 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]
    }

}

I am trying to make it so it can be combined with more arrays like this: 我正在尝试使其能够与更多这样的数组结合使用:

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]
    }

}

So it does combine them and takes RANDOM out of them all but I can set criterias like it must have one at least from every "List". 因此它确实将它们组合在一起,并从它们中去除了RANDOM,但是我可以设置条件,例如每个“列表”中至少必须有一个。 Perfect would be: Every week at least once —> Meat, Fish, Vegetables and then the rest is random from the first list... 完美的做法是:每周至少一次->肉,鱼,蔬菜,然后其余的从第一个列表中随机抽取...

I hope you guys can help me :) 我希望你们能帮助我:)

Kind regards Alex 亲切的问候亚历克斯

Albeit this may not be exactly what you are looking for, you could try the following: 尽管这可能并非您要找的东西,但是您可以尝试以下方法:

{
# 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]
}

The $Foods variable of course is not an Enum type but an object collection, however you can then generate your random 'meal' of the day & have the option to extend the list as additional categories are added. $ Foods变量当然不是Enum类型,而是对象集合,但是您可以随后生成当天的随机“餐”,并可以选择添加其他类别来扩展列表。 To access a specific entry you can index as follows: $Foods[10] 要访问特定条目,您可以按如下所示编制索引:$ Foods [10]

The current variable contains 19 elements ($Foods.count) 当前变量包含19个元素($ Foods.count)

Hope it helps, 希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM