Home > Enterprise >  Why won't this array property populate
Why won't this array property populate

Time:10-01

I have two multidimensional arrays that have some shared data. The elements are not in the same order between the two arrays, nor do the property names match in every instance. However, the fields to compare will always have the same name (i.e. $Asset in Array1 and $Name in $Array2). What I want is for the a property in Array1 to be populated when there is a partial match in Array2. Below is an example.

$Array1 = @(
    @{Asset = '123'; Region = 'Earth'; User = ''}
    @{Asset = '098'; Region = 'Moon'; User = 'Carla'}
    )


$Array2 = @(
    @{Name= '123456'; User = 'Steve'; Date = '1/1/22'}
    )

What I would like is for the script to find the partial match (123) and, based on that, populate $Array1.user with "Steve". It seems like this should be an easy thing to do with a ForEach loop, but I cannot work out how to do it. Below are some samples of things I've tried.

ForEach ($Comp in $Array1)
{
    $Array2 -like "*$Comp*"
    $Comp.user = ($Array2).user
  }

_______________

ForEach ($Comp in $Array1)
{
     $Comp -match $Array2
     $Comp.user = $($Array2).user
} 

   

I have tried different combinations of nested ForEach loops, ForEach loops with If statements, etc. I'm sure this is an easy fix, but I am new to PS and am not seeing my problem. Could you please help? It's the only thing keeping my script from being complete.

Thanks in advance!

CodePudding user response:

Just to clarify some terminology first, you've not actually got multidimensional arrays - you'd declare those with something like $x = new-object "string[,]" 2, 2 and then you'd have to poke each value into the array individually to initialise it with $x[0, 0] = "aaa"; $x[0, 1] = "bbb"; ... etc

What you've really got is two single-dimension arrays where each item in the array is a hashtable.

Having said that, a simple way to do what you want is with this imperative snippet:

# set up some test data
$Array1 = @(
    @{ "Asset" = "123"; "Region" = "Earth"; "User" = "" },
    @{ "Asset" = "098"; "Region" = "Moon";  "User" = "Carla"}
)
$Array2 = @(
    @{ "Name" = "123456"; "User" = "Steve"; "Date" = "010/1/22" }
)

# update the user details
foreach( $item1 in $Array1 )
{
    foreach( $item2 in $Array2 )
    {
        if( $item2.Name -like "$($item1.Asset)*" )
        {
            $item1.User = $item2.User
        }
    }
}

# show the result
$Array1 | ConvertTo-Json
# [
#   {
#     "Region": "Earth",
#     "User": "Steve",     # <-- was "", is now "Steve"
#     "Asset": "123"
#   },
#   {
#     "Region": "Moon",
#     "User": "Carla",
#     "Asset": "098"
#   }
# ]

The nested foreach loops basically compare every item in $Array1 with every item in $Array2 and update the item from $Array1 when the if expression is true.

There's probably some slightly slicker approaches with a pipeline, but the code above gets the job done :-).

  • Related