-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathswitch-hides-current-item.ps1
More file actions
32 lines (25 loc) · 850 Bytes
/
switch-hides-current-item.ps1
File metadata and controls
32 lines (25 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<#
.Synopsis
"switch" current object hides "ForEach-Object" current object.
.Description
If the idea is for each word to print the word and tell if its length is 3
or not 3 then this script is not correct. "$_" inside "switch" is not the
current word, it is the current switch value, the word length.
The correct code which still uses "switch" has to store the current object
$_ to another variable $word and use this variable inside "switch":
$words | ForEach-Object {
$word = $_
switch($_.Length) {
3 { "Word '$word' length is 3." }
default { "Word '$word' length is not 3." }
}
}
Or "switch" may be replaced with "if" statements to avoid this issue.
#>
$words = 'one', 'two', 'three'
$words | ForEach-Object {
switch($_.Length) {
3 { "Word '$_' length is 3." }
default { "Word '$_' length is not 3." }
}
}