Performing Comparisons Inside Arrays

Performing Comparisons Inside Arrays

Use this for easy navigation to main pages. This is hidden on your site
Delete me after reading
Post & Video Instructions - This is hidden on your site
  • Latest Posts & Videos, All Posts, and All Videos are served from the CONTENT DATABASE, based on a few properties:
  • Latest Posts & Videos
    1. Type: Post or Video plus Feature As a Feature post or video, it also serves to the home page as a Latest Post or Latest Video. Consider only having 1-3 Feature at a time to avoid cluttering the page.
    2. Status: Live - a post or video marked as Draft will not serve to this page or the home page.
    image
    All Posts or All Videos
    1. Type: Post or Video
    2. Status: Live
  • Make sure to replace the text in the properties: Title, Excerpt, and Published Date so Post display correctly like this:
  • image

Comparisons Inside Arrays - Useful and Fancy!

I recently got some assistance from the ALWAYS helpful Joel Tallow with looking for ways to improve my code. He looked through some code and gave me some improvements. He also showed me some really cool bits of code and this post will cover one that I thought was useful.

# what does this code do? Let's find out!
$selection = @($preference, "Default" -ne $null)[0]

Quick Refresher

Let’s do a quick review of arrays and comparison operators if they aren’t fresh in your mind.

Arrays

# This creates an array of 2 values. Arrays are good at holding multiple values
@('value1','Value2')

# To verify that this is of the array type, Run
@('value1','value2') -is [array]

Array Indexing

# We can use an index number to select which value we want out of the array
# We put the index number inside []
# [0] returns the first  value in the array, which is value1.
@('value1','Value2')[0]

# [1] returns the second value in the array (counting starts at 0), which is value2.
@('value1','value2','value3')[1]

# to get the last value in the array, we specify [-1]
@('value1','value2','value3')[1]

Comparison Operators

# Comparison operators compare things and then tell us if the comparison was true or false.
# Is 'Toby' the same string as 'Scranton Strangler'?
'Toby' -eq 'Scranton Strangler'
# darn, we almost had him.

'Pinocchio' -eq 'Real Boy'
# ouch, poor guy

# We can also determine if things are Null or not
$null -eq 'Michael'

5 -lt 6

# We haven't defined the NonExistentVariable yet, so it doesn't exist yet.
$null -eq $NonExistentVariable

Comparison Operators Within Arrays

# Let's have an array and put a comparison operator in it.
# This allows us to sort of filter
@(1,2,3 -GT 1)

@('','notBlank' -notlike '')

# Create an array without any null values.
@($NonExistentVariable, 'string' -ne $null)

Code Time

# We need to select a new office color. We would like to use the color in $preference.
# if $preference is null, then we will return 'CorporateGrayscale'
$newOfficeColor = @($preference, "CorporateGrayscale" -ne $null)[0]
# We have an array with a comparison. This effectively filters out any null values.
# we then select the [0] value, which will either be $preference or "corporateGrayscal"
# I hope that someone defined $preference 0_O

# This is how I traditionally would have handled something like this
if ($preference){
    $newOfficeColor = $preference
}
else {
    $newOfficeColor = "CorporateGrayscale"
}


# This is the actual code that was updated
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($File)
$ContentType = @($mimeType, "application/octet-stream" -ne $null)[0]

# Old long code
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($File)
if ($mimeType) {
    $ContentType = $mimeType
}
else {
    $ContentType = "application/octet-stream"
}

Read More

📄
Content

NameExcerpt
1
2
© 2022 AndrewPla.Tech
3
Take care of yourself because you deserve it