How does an array treat a variable if it’s commented out?

Welcome Forums General PowerShell Q&A How does an array treat a variable if it’s commented out?

Viewing 6 reply threads
  • Author
    Posts
    • #205008
      Participant
      Topics: 2
      Replies: 5
      Points: 29
      Rank: Member

      I was just testing and learning and need some clarification on how an array treats a variable if it’s commented out higher in the code.  Example:

      $value1 = "1"
      #$value2 = "2"
      #$value3 = "3"
      $array = @($value1,$value2,$value3)

      I did some testing using $array[0] (and 1,2) and it always displays $value1 which would make sense.
      So are these values ignored?

      • This topic was modified 3 months, 1 week ago by grokkit.
    • #205035
      Senior Moderator
      Topics: 3
      Replies: 123
      Points: 653
      Helping Hand
      Rank: Major Contributor

      It seems that essentially you’re asking if $null values will be added to the array or not. The answer is yes – if you assign an empty or nonexistent variable to an array, then the array element for that position (whatever it should be for that array) will be added to the array with a value of $null. You can see this happen like this:

      $a = 1
      #$b = 2
      $c = 3
      
      $array = @( $a, $b, $c )
      
      echo $array
      1
      3
      
      echo $array.Count
      3

      The count shows that there are 3 elements in the array, even though echoing the contents of the array only shows the values for $a and $c. So, even though $b has not been assigned a value, it was still accounted for when the array was created. When the shell called $b while creating the array, it found that the value of $b was $null and assigned that value to the array element (the shell follows your instructions exactly, even if your instructions are wrong or don’t make sense).

      This is true in general – any time that you call a value that does not exist (variable/array/array element/property/etc), the value is $null. This is something that has to be handled carefully, as it can creep into your script due to a typo (e.g. you have a variable $beta but you referenced $bta instead and now your script is using a value of $null). Your script can continue operating with no obvious problem until the $null value causes something else to fail, which may be difficult to track down when troubleshooting.

      Here are some more thorough explanations:
      Powershell: Everything you wanted to know about arrays: $null or empty
      Powershell: Everything you wanted to know about $null

      • This reply was modified 3 months, 1 week ago by grokkit.
    • #205107
      Participant
      Topics: 2
      Replies: 5
      Points: 29
      Rank: Member

      Thank you for the explanation and examples.   I have one more question I didn’t find info for in the links provided.
      What if you wanted to test for a value that’s not null?   How would this be done?
      Example – does not work, I also tried like and contains:

      #$a = 1
      $b = 2
      #$c = 3
      $array = @( $a, $b, $c )
      if  ($array -eq "1")
      {
      Write-Host "value 1"
      }
      
      if  ($array -eq "2")
      {
      Write-Host "value 2"
      }
      if  ($array -eq "3")
      {
      Write-Host "value 3"
      }
      • This reply was modified 3 months, 1 week ago by grokkit. Reason: please format your code
    • #205131
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      You can just clean the array to ensure there are no value NULL values:

      PS C:\Users\rasim> 
      #$a = 1
      $b = 2
      #$c = 3
      $array = @( $a, $b, $c )
      
      PS C:\Users\rasim> $array.Count
      3
      
      PS C:\Users\rasim> $arraysWithNoNull = $array | Where{$_}
      
      PS C:\Users\rasim> $arraysWithNoNull.Count
      1
      
    • #205194
      Participant
      Topics: 2
      Replies: 5
      Points: 29
      Rank: Member

      Also testing your original example on my computer does not work the same for me?  Is there a version dependency?  It’s still seeing the nulls.

      $a = 1
      #$b = 2
      $c = 3
      $array = @( $a, $b, $c )
      echo $array

      Produces a result of:
      1
      2
      3

      • This reply was modified 3 months, 1 week ago by grokkit.
    • #205209
      Senior Moderator
      Topics: 3
      Replies: 123
      Points: 653
      Helping Hand
      Rank: Major Contributor

      It’s still seeing the nulls.

      This is not really correct. By commenting out the variable assignment, you are not changing the value of $b. You have not done anything to $b. This is not the same thing as assigning $b a value of $null. If $b contained data before, it still does now. If you want to set $b to a value of $null (remove the value of $b) then you have to do it explicitly:

      $b = $null

      Your $array also needs to be cleared if you are using it again.

      Even if you know that you want an array or variable to be reset, the shell does not know that unless you tell it.

    • #205224
      Participant
      Topics: 2
      Replies: 5
      Points: 29
      Rank: Member

      Thanks again, this is what I was missing “clearing the array and variables”.   When I added this to the script is works as expected.

Viewing 6 reply threads
  • The topic ‘How does an array treat a variable if it’s commented out?’ is closed to new replies.