I’m having a problem with the error routines I’m trying to rewrite for some scripts. The error codes are captured to arrays, and then used as parameters in if statements for reporting. If there are errors, then all is fine, but if everything works OK in the processes, the reporting errors out. I think I’ve determined that the problem is the way I’m testing the arrays, but I’m not sure. I came up with this quick script to test the value of an empty array, and it leads me to believe there’s something special about an empty array.<\/p>\n
$SkippedFiles = @()\nif ($SkippedFiles -eq 0)\n{\n Write-Host 'Empty arrays = 0'\n} \nelseif ($SkippedFiles -eq $null) \n{\n Write-Host 'Empty arrays = $null'\n} \nelse \n{\n Write-Host 'Empty arrays are neither $null nor 0'\n}\n\n<\/code><\/pre>\n
Advertisement
The answer I get is (you guessed it)<\/p>\n
Empty arrays are neither $null nor 0\n\n<\/code><\/pre>\n
Advertisement
So just what is the value of an empty array and how do you test for it?<\/p>","upvoteCount":4,"answerCount":7,"datePublished":"2018-08-03T15:17:48.000Z","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"},"acceptedAnswer":{"@type":"Answer","text":"
You can just try<\/p>\n
$SkippedFiles = @()\nif ($SkippedFiles) {\n Write-Host 'Not empty'\n} \nelse {\n Write-Host 'Empty arrays'\n}\n<\/code><\/pre>\nIF will check if it is null or empty<\/p>\n
Why are you trying to differentiate? Either way the array is empty.
\nIts an array, so you’re looking for Count to test for contents.<\/p>\n
Maybe I’m not understanding correctly, sorry.<\/p>","upvoteCount":1,"datePublished":"2018-08-03T15:23:41.000Z","url":"https://community.spiceworks.com/t/whats-the-value-of-an-empty-array/665897/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
I’m having a problem with the error routines I’m trying to rewrite for some scripts. The error codes are captured to arrays, and then used as parameters in if statements for reporting. If there are errors, then all is fine, but if everything works OK in the processes, the reporting errors out. I think I’ve determined that the problem is the way I’m testing the arrays, but I’m not sure. I came up with this quick script to test the value of an empty array, and it leads me to believe there’s something special about an empty array.<\/p>\n
$SkippedFiles = @()\nif ($SkippedFiles -eq 0)\n{\n Write-Host 'Empty arrays = 0'\n} \nelseif ($SkippedFiles -eq $null) \n{\n Write-Host 'Empty arrays = $null'\n} \nelse \n{\n Write-Host 'Empty arrays are neither $null nor 0'\n}\n\n<\/code><\/pre>\nThe answer I get is (you guessed it)<\/p>\n
Empty arrays are neither $null nor 0\n\n<\/code><\/pre>\nSo just what is the value of an empty array and how do you test for it?<\/p>","upvoteCount":4,"datePublished":"2018-08-03T15:17:48.000Z","url":"https://community.spiceworks.com/t/whats-the-value-of-an-empty-array/665897/1","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"}},{"@type":"Answer","text":"