brehma
brehma

Reputation: 206

Why do foreach loop variables show 1 set when there should be more

I imported a CSV file and reduced the imported information to contain 2 rows of data ($updateAnchorage), and when I call $updateAnchorage, it contains the data I would expect. Then I created 2 hash tables from the imported data using a foreach loop, as I will show you below. I am trying to understand PowerShell better, so my question is why does $row in the foreach loop only give a single row of data when I would expect it to show 2 rows?

Here is the code and results:

$updateA = Import-Csv -Path C:\Windows\System32\MyFile-CSV.csv `
        -Header "name","old","new" | select -Skip 2
$hashIP = @{}
$hashName = @{}

foreach($row in $updateA)
{
    $hashIP[$row.old] = $row.new
    $hashName[$row.new] = $row.Name   
}

Here is the output of $updateA and $rows:

PS C:\Users\a> $updateA name old new
---- --- ---
City - HP LaserJet 4250N - Sales... 192.168.2.120 192.168.2.50
City - Lexmark XM1145 - Sales Co... 192.168.2.106 192.168.2.51

PS C:\Users\a> $row

name old new
---- --- ---
City - Lexmark XM1145 - Sales Co... 192.168.2.106 192.168.2.51

Upvotes: 1

Views: 40

Answers (1)

omniomi
omniomi

Reputation: 130

$Row will be the last thing to go through the foreach construct. You need to call either $hashIP or $hashName.

This is what your script creates:

[nickj@nick-lt scripts]$ $hashIP

Name                           Value                                                                                                                                                                                                                 
----                           -----                                                                                                                                                                                                                 
2.2.2.2                        2.2.2.3                                                                                                                                                                                                               
1.1.1.1                        1.1.1.2                                                                                                                                                                                                               

[nickj@nick-lt scripts]$ $hashName

Name                           Value                                                                                                                                                                                                                 
----                           -----                                                                                                                                                                                                                 
2.2.2.3                        Name2                                                                                                                                                                                                                 
1.1.1.2                        Name1   

My CSV:

"Name","Old","New"
Name1,1.1.1.1,1.1.1.2
Name2,2.2.2.2,2.2.2.3

If you did something simple like this:

$Array = 1,2,3,4,5

foreach ($Num in $Array)
{
    continue
}

When it is done running $Num which is equivelent to your $Row would be '5' as it would be the last thing from the array to pass through the foreach construct. It is essentially doing this:

$Array = 1,2,3,4,5

$Num = $Array[0]
# Continue

$Num = $Array[1]
# Continue

$Num = $Array[2]
# Continue

$Num = $Array[3]
# Continue

$Num = $Array[4]
# Continue

So through each loop $Num is set to the current array value it's working on. If you picture it as foreach (<WHAT> in <COLLECTION>) {} you should only ever reference <WHAT> inside the foreach's braces. It becomes useless to you outside.

Upvotes: 1

Related Questions