mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[powershell/en] Fix and clarify -eq operator on (some) objects (#4333)
This commit is contained in:
parent
51efc47933
commit
d123cd4a5c
@ -118,14 +118,15 @@ $False - 5 # => -5
|
|||||||
2 -lt 3 -and 3 -lt 2 # => False
|
2 -lt 3 -and 3 -lt 2 # => False
|
||||||
|
|
||||||
# (-is vs. -eq) -is checks if two objects are the same type.
|
# (-is vs. -eq) -is checks if two objects are the same type.
|
||||||
# -eq checks if the objects have the same values.
|
# -eq checks if the objects have the same values, but sometimes doesn't work
|
||||||
|
# as expected.
|
||||||
# Note: we called '[Math]' from .NET previously without the preceeding
|
# Note: we called '[Math]' from .NET previously without the preceeding
|
||||||
# namespaces. We can do the same with [Collections.ArrayList] if preferred.
|
# namespaces. We can do the same with [Collections.ArrayList] if preferred.
|
||||||
[System.Collections.ArrayList]$a = @() # Point a at a new list
|
[System.Collections.ArrayList]$a = @() # Point a at a new list
|
||||||
$a = (1,2,3,4)
|
$a = (1,2,3,4)
|
||||||
$b = $a # => Point b at what a is pointing to
|
$b = $a # => Point b at what a is pointing to
|
||||||
$b -is $a.GetType() # => True, a and b equal same type
|
$b -is $a.GetType() # => True, a and b equal same type
|
||||||
$b -eq $a # => True, a and b values are equal
|
$b -eq $a # => None! See below
|
||||||
[System.Collections.Hashtable]$b = @{} # => Point a at a new hash table
|
[System.Collections.Hashtable]$b = @{} # => Point a at a new hash table
|
||||||
$b = @{'one' = 1
|
$b = @{'one' = 1
|
||||||
'two' = 2}
|
'two' = 2}
|
||||||
@ -154,6 +155,13 @@ $age = 22
|
|||||||
"$name's name is $($name.Length) characters long."
|
"$name's name is $($name.Length) characters long."
|
||||||
# => "Steve's name is 5 characters long."
|
# => "Steve's name is 5 characters long."
|
||||||
|
|
||||||
|
# Strings can be compared with -eq, but are case insensitive. We can
|
||||||
|
# force with -ceq or -ieq.
|
||||||
|
"ab" -eq "ab" # => True
|
||||||
|
"ab" -eq "AB" # => True!
|
||||||
|
"ab" -ceq "AB" # => False
|
||||||
|
"ab" -ieq "AB" # => True
|
||||||
|
|
||||||
# Escape Characters in Powershell
|
# Escape Characters in Powershell
|
||||||
# Many languages use the '\', but Windows uses this character for
|
# Many languages use the '\', but Windows uses this character for
|
||||||
# file paths. Powershell thus uses '`' to escape characters
|
# file paths. Powershell thus uses '`' to escape characters
|
||||||
@ -274,6 +282,10 @@ $array.AddRange($otherArray) # Now $array is [1, 2, 3, 4, 5, 6]
|
|||||||
# Examine length with "Count" (Note: "Length" on arrayList = each items length)
|
# Examine length with "Count" (Note: "Length" on arrayList = each items length)
|
||||||
$array.Count # => 6
|
$array.Count # => 6
|
||||||
|
|
||||||
|
# -eq doesn't compare array but extract the matching elements
|
||||||
|
$array = 1,2,3,1,1
|
||||||
|
$array -eq 1 # => 1,1,1
|
||||||
|
($array -eq 1).Count # => 3
|
||||||
|
|
||||||
# Tuples are like arrays but are immutable.
|
# Tuples are like arrays but are immutable.
|
||||||
# To use Tuples in powershell, you must use the .NET tuple class.
|
# To use Tuples in powershell, you must use the .NET tuple class.
|
||||||
|
Loading…
Reference in New Issue
Block a user