Reputation: 787
trying to work this one out and not having much luck really. The if statement from the following block of code is never true, which I don't understand:
$array1 = "red", "blue", "green", "yellow"
$array2 = "rose", "sky", "grass", "sand"
$both_arrays = $array1, $array2
for ($c=0; $c -lt 4; $c++) {
$object = $both_arrays[0][$c]
$colour = $both_arrays[1][$c]
Write-Host $c "-" $object
Write-Host $c "-" $colour
if ( ($colour.contains("green") ) ) { Write-Host "HELL YEAH! Green is my favourite colour!" }
}
Output as follows:
0 - red
0 - rose
1 - blue
1 - sky
2 - green
2 - grass
3 - yellow
3 - sand
It works fine if we're just dealing with one array though:
$array1 = "red", "blue", "green", "yellow"
for ($c=0; $c -lt 4; $c++) {
$colour = $array1[$c]
Write-Host $c "-" $colour
if ( ($colour.contains("green") ) ) { Write-Host "HELL YEAH! Green is my favourite colour!" }
}
Output comes out as expected:
0 - red
1 - blue
2 - green
HELL YEAH! Green is my favourite colour!
3 - yellow
What the smeg is going on? Help appreciated!
Still getting an error with an example of the above code when I attempt to apply the concept to another script I'm writing:
for ($c=0; $c -lt 3; $c++) { #$arrCheckpoint,
$ckname = $arrCheckpointNameAndStatus[0][$c]
$ckstatus = $arrCheckpointNameAndStatus[1][$c]
Write-Host $ckname
if ( ($ckname.contains("Standard Checkpoint") ) ) { Write-Host "Hit." }
}
Write-Host "End of Script"
It throws the following error:
Method invocation failed because [System.Object[]] doesn't contain a method named 'contains'.
At C:\script.ps1:225 char:27
+ if ( ($ckname.contains <<<< ("Standard Checkpoint") ) ) { Write-Host "Hit." }
+ CategoryInfo : InvalidOperation: (contains:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Really starting to bug me now!
Upvotes: 0
Views: 2832
Reputation: 68263
You appear to have the array indexing of your object and color arrays reversed.
Upvotes: 1