我怎样才能回到位置嵌套数组中,从第一个元素的价值? [英] How can I return the position a nested array is in, from the value of the first element?

查看:102
本文介绍了我怎样才能回到位置嵌套数组中,从第一个元素的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这标题很有意义!

Hopefully that title makes sense!

我有一个数组,这是由48台3号。我现在想做的是在pre定义的顺序打印这些组3个数字的基础上,在这一套3.第一个号码的子数组像这样:

I have an array, which is made up of 48 sets of 3 numbers. What I now want to do is print these sets of 3 numbers in a pre-defined order, based on the first number in that set of 3. The sub-array is like so:

@($label, $number1, $number2)

有将48套的这些,标签总是唯一的,并且从CSV文件的标题拉动。 (CSV文件时通过各种PowerShell脚本送入整理他们的头衔,并将其转换为XLS,并就运行各种宏)他们通过在文件顺序,但我想打印出来在另一$ P $定义对为了我的选择的。

There will be 48 sets of these, the label is always unique and is pulled from the title of a CSV file. (The CSV files are fed through various PowerShell scripts to tidy their titles and convert them to XLS, and to run various macros on.) They go through in file order, but I want to print them out in another pre-defined order of my choosing.

因此​​,为了打印出来的标签中我想的顺序,我需要能够测试子阵列的$标签的一部分。如果我想要的标签相匹配,打印组3出。

So in order to print them in the order of label I would like, I need to be able to test the $label part of the sub-array. If it matches the label I want, print the set of 3 out.

要考验我的code到目前为止,我已经建立了一个循环,填充我的数组:

To test my code so far I have constructed a loop that populates my array:

$results = @(1 .. 48)

$label = 148
$var1 = 5
$var2 = 3

for($i = 0; $i -le 47; $i++) {
    $results[$i] = @($label, $var1, $var2)
}

我现在需要做的是打印这些特定的顺序,我会由标签决定套3。 code的下一节将可能只是像posArray($标签)的自定义功能。当我这个打出来我意识到这将是一种有益的打印,标签的阵列的一部分,它不必一定告诉我是哪个位置。

What I need to do now is print these sets of 3 in a particular order, which I will dictate by the label. The next section of code will probably just be a custom function like posArray($label). As I type this out I realise it would be just as useful to print the part of the array with the label in, it doesn't have to necessarily tell me which position it is.

请随时提出建筑师这个如果可能的话更好的方法。它必须在PowerShell中,虽然运行。我想过,也许一个散列,但因为它的两大增值经销商并非仅仅是一,我不知道。

Please feel free to suggest better ways to architect this if possible. It must run in PowerShell though. I thought about maybe a hash, but as it's two vars not just a pair I wasn't sure.

我试过回去一轮循环,并在每个嵌套的数组,检查的第一要素,但它打印出System.Object的[]时代的负载。

I've tried to go back round the loop and check the first element in each of the nested arrays, but it prints out "System.Object[]" a load of times.

for($i = 0; $i -le 47; $i++) {
    if($results[$i][0] -eq $label) {
        Write-Host "$results[$i]"
    }
}

所以,我真的不知道发生了什么没有。

So I'm not really sure what's happening there.

如果在理想情况下是这样的情况下(for循环搜索和打印1组由$标签),我把循环中的函数,调用由functionName($标签)功能,我会调用该函数一堆与我期望的顺序标签倍。

Ideally if this was the case (the for loop searching and printing the 1 set by $label) I'd put the loop in a function, call the function by FunctionName($label) and I would call the function a bunch of times with the labels in my desired order.

征文结束了...

推荐答案

相反嵌套的数组,可以工作的,我想你会与自定义对象一个更好的体验。这应该在2.0工作

Instead of nested arrays, which could work, I think your would have a much better experience with custom objects. This should work in 2.0

$dasData = 0..47 | ForEach-Object{
    New-Object -TypeName psobject -Property @{
        # Create an object with a nested array.
        Index = $_
        Details = [char]([int]$_ + 65),(Get-Random -Minimum 0 -Maximum 50),(Get-Random -Minimum 0 -Maximum 50)
    }
}

我只是做了一个对象数组,其中每个对象都有一个指数和3个值的数组。忽略实际值。这只是一些ANSI可打印字符和随机数。综观第一对夫妇 $ dasData的元素 ...

Details     Index
-------     -----
{A, 15, 6}      0
{B, 28, 31}     1
{C, 41, 23}     2
{D, 0, 7}       3

所以,现在供电,这些信息我们可以只使用标准的PowerShell命令来获得你正在寻找的信息。

So now powered with that information we can just use standard PowerShell cmdlets to get the information you are looking for.

$dasData | Where-Object{$_.Details[0] -ceq "F"}

我们正在寻找的地方细节的第一个元素是等于资本F.这将在数组中返回第5个元素的元素相匹配的数据。

We are looking for the data that matches the element where the first element of details is equal to capital F. That would return the 5th element in the array.

Details     Index
-------     -----
{F, 49, 43}     5

如果你真的只是在指数感兴趣那么一个简单的选择会得到你。

And if you were really just interested in the index then a simple select would get that for you.

$dasData | Where-Object{$_.Details[0] -ceq "F"} | Select-Object -ExpandProperty Index


在你的第二个$ C $账套你是不是要求正确,因为你是投作为一个字符串的元素。


In your second code set you are not calling the element properly because you are casting it as a string.

Write-Host "$results[$i]" 

而应该是以下的。

Should instead be either of the following.

Write-Host "$($results[$i])" 
Write-Host $results[$i]

这篇关于我怎样才能回到位置嵌套数组中,从第一个元素的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆