mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Addressing comments
1. Clarifying '[]' and null-assigned expressions. 2. Removing "-contains" on switch example from Wouter, it does not appear to be true anymore 3. Adding comments and adjusting some func examples a bit
This commit is contained in:
parent
5c79b4288c
commit
e6a53387be
@ -149,25 +149,49 @@ $age = 22
|
|||||||
# $null is not an object
|
# $null is not an object
|
||||||
$null # => None
|
$null # => None
|
||||||
|
|
||||||
|
# $null, 0, and empty strings and arrays all evaluate to False.
|
||||||
|
# All other values are True
|
||||||
# $null, 0, and empty strings and arrays all evaluate to False.
|
# $null, 0, and empty strings and arrays all evaluate to False.
|
||||||
# All other values are True
|
# All other values are True
|
||||||
function test ($value) {
|
function test ($value) {
|
||||||
if ($value) {Write-Output 'True'}
|
if ($value) {
|
||||||
else {Write-Output 'False'}
|
Write-Output 'True'
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Write-Output 'False'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test ($null) # => False
|
test ($null) # => False
|
||||||
test (0) # => False
|
test (0) # => False
|
||||||
test ("") # => False
|
test ("") # => False
|
||||||
test [] # => True
|
test [] # => True *[] Not valid in Powershell, creates null-valued expression
|
||||||
test ({}) # => True
|
test ({}) # => True
|
||||||
test @() # => False
|
test @() # => False
|
||||||
|
|
||||||
|
<#
|
||||||
|
IsPublic IsSerial Name BaseType
|
||||||
|
-------- -------- ---- --------
|
||||||
|
True True Int32 System.ValueType
|
||||||
|
True True Int32 System.ValueType
|
||||||
|
True True String System.Object
|
||||||
|
You cannot call a method on a null-valued expression.
|
||||||
|
At line:4 char:1
|
||||||
|
+ $test3.getType()
|
||||||
|
+ ~~~~~~~~~~~~~~~~
|
||||||
|
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
|
||||||
|
+ FullyQualifiedErrorId : InvokeMethodOnNull
|
||||||
|
|
||||||
|
True True ScriptBlock System.Object
|
||||||
|
True True Object[] System.Array
|
||||||
|
#>
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
## 2. Variables and Collections
|
## 2. Variables and Collections
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# Powershell uses the "Write-Output" function to print
|
# Powershell uses the "Write-Output" function to print
|
||||||
Write-Output "I'm Powershell. Nice to meet you!" # => I'm Powershell. Nice to meet you!
|
Write-Output "I'm Posh. Nice to meet you!" # => I'm Posh. Nice to meet you!
|
||||||
|
|
||||||
# Simple way to get input data from console
|
# Simple way to get input data from console
|
||||||
$userInput = Read-Host "Enter some data: " # Returns the data as a string
|
$userInput = Read-Host "Enter some data: " # Returns the data as a string
|
||||||
@ -367,7 +391,6 @@ switch($val) {
|
|||||||
{ $_ -like 's*' } { "Case insensitive"; break }
|
{ $_ -like 's*' } { "Case insensitive"; break }
|
||||||
{ $_ -clike 's*'} { "clike, ceq, cne for case sensitive"; break }
|
{ $_ -clike 's*'} { "clike, ceq, cne for case sensitive"; break }
|
||||||
{ $_ -notmatch '^.*$'} { "Regex matching. cnotmatch, cnotlike, ..."; break }
|
{ $_ -notmatch '^.*$'} { "Regex matching. cnotmatch, cnotlike, ..."; break }
|
||||||
{ 'x' -contains 'x'} { "FALSE! -contains is for lists!"; break }
|
|
||||||
default { "Others" }
|
default { "Others" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,7 +407,6 @@ finally {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Writing to a file
|
# Writing to a file
|
||||||
$contents = @{"aa"= 12
|
$contents = @{"aa"= 12
|
||||||
"bb"= 21}
|
"bb"= 21}
|
||||||
@ -532,6 +554,7 @@ True False Guitar Instrument
|
|||||||
#>
|
#>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
## 7. Advanced
|
## 7. Advanced
|
||||||
####################################################
|
####################################################
|
||||||
@ -582,7 +605,7 @@ $Area
|
|||||||
This is a silly one
|
This is a silly one
|
||||||
You may one day be asked to create a func that could take $start and $end
|
You may one day be asked to create a func that could take $start and $end
|
||||||
and reverse anything in an array within the given range
|
and reverse anything in an array within the given range
|
||||||
based on an arbitrary array
|
based on an arbitrary array without mutating the original array
|
||||||
Let's see one way to do that and introduce another data structure
|
Let's see one way to do that and introduce another data structure
|
||||||
#>
|
#>
|
||||||
|
|
||||||
@ -599,7 +622,7 @@ function Format-Range ($start, $end) {
|
|||||||
elseif ($index -ge $start -and $index -le $end) {
|
elseif ($index -ge $start -and $index -le $end) {
|
||||||
$stack.Push($targetArray[$index])
|
$stack.Push($targetArray[$index])
|
||||||
}
|
}
|
||||||
elseif ($index -gt $end) {
|
else {
|
||||||
$secondSectionArray.Add($targetArray[$index]) > $null
|
$secondSectionArray.Add($targetArray[$index]) > $null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -607,6 +630,8 @@ function Format-Range ($start, $end) {
|
|||||||
Write-Output $returnArray
|
Write-Output $returnArray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Format-Range 2 6 # => 'a','b','g','f','e','d','c','h','i','j','k','l','m','n'
|
||||||
|
|
||||||
# The previous method works, but it uses extra memory by allocating new arrays
|
# The previous method works, but it uses extra memory by allocating new arrays
|
||||||
# It's also kind of lengthy
|
# It's also kind of lengthy
|
||||||
# Let's see how we can do this without allocating a new array
|
# Let's see how we can do this without allocating a new array
|
||||||
@ -623,10 +648,13 @@ function Format-Range ($start, $end) {
|
|||||||
}
|
}
|
||||||
return $targetArray
|
return $targetArray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Format-Range 2 6 # => 'a','b','g','f','e','d','c','h','i','j','k','l','m','n'
|
||||||
```
|
```
|
||||||
Powershell as a Tool:
|
Powershell as a Tool:
|
||||||
|
|
||||||
Getting Help:
|
Getting Help:
|
||||||
|
|
||||||
```Powershell
|
```Powershell
|
||||||
# Find commands
|
# Find commands
|
||||||
Get-Command about_* # alias: gcm
|
Get-Command about_* # alias: gcm
|
||||||
|
Loading…
Reference in New Issue
Block a user