Hopefully that title makes sense!
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)
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.
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.
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)
}
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.
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.
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.
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.
Essay over...
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
I just made an object array where each object has an index and an array of 3 values. Ignore the actual values. It is just some ANSI printable characters and random numbers. Looking at the first couple of elements of
$dasData
...So now powered with that information we can just use standard PowerShell cmdlets to get the information you are looking for.
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.
And if you were really just interested in the
index
then a simple select would get that for you.In your second code set you are not calling the element properly because you are casting it as a string.
Should instead be either of the following.