Introduction to Arrays
Introduction to Arrays
A Series of Items
Introduction
A list is a group of things or objects. Traditionally, the things or objects in the array are of the same type such as a group of names, a group of houses, a group of countries, etc. The things or objects in an array can be referred to as items or members. There are various types of lists.
An array is a list of items where each item occupies a specific position. There are various ways you can create or start an array.
An Array as a List
The simplest technique to create an array is as follows:
$variable-name = item-1 , item-2,. . . , item-x
Basedon this formula, to creating an array, start with a name, which consists of the $ symbol followed by a letter or a combination of letters, digits, and underscore. Follow it by a space. Then type each item. Separate them with comas. Assign that list to the variable you had declared. The items of an array can be numbers, characters/symbols, strings, boolearn values (one or a combination of $true and $false), objects, etc. If a member is a number, simply type it. If a member is a Boolean value, type it as $true or as $false. If a member is character, a symbol or a string, include it in single or double-quotes. Here is an example:
$customers = "Thomas Stones", 'Ericka Dellaney', "Kelly Davids", 'Marianne Harrington'
One of the formulas to create an array is:
$variable-name = @( item1, item2, ..., item-x)
Based on this formula, start by declaring a variable using the $ symbol followed by a letter or a group of letters, digits, and underscores. Assign @() to the variable. In the parentheses, put the itmes you want, separate them with commas. Here is an example:
$numbers = @(12.44, 525.38, 6.28, 2448.32, 632.04)
A Vertical Array
Notice that, in the above array variables, all items were created on the same line, and they were separated by comas. Another technique to create an array is to put each item on its own line. Here is an example:
$numbers = @(
12.44,
525.38,
6.28,
2448.32,
632.04
)
If you use this technique and if the array is made of numbers, you can omit the comas. Here is an example that uses numbers:
$numbers = @(
12.44
525.38
6.28
2448.32
632.04
)
If you use this technique of aligning the items vertically, if the array is made of strings, you should (must) separate the items with comas.
Primary Techniques to Access an Array
Accessing the Variable of an Array
There are various ways you can use an array. The simplest way is to access the whole array from its variable. To do that, simply type the name of the array. Here is an example:
$numbers = @(
12.44,
525.38,
6.28,
2448.32,
632.04
)
$numbers
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 12.44 525.38 6.28 2448.32 632.04
In the second lesson, we saw that you can use Write-Host to display a value on the computer screen. In the same way, to display an array as an object, you can write it outside the single or double-quotes of Write-Host. If you had declared a variable and initialized it with an array, you can include that array in the quotes of Write-Host. Here are examples:
PS C:\Windows\System32> $states = @("New Hampshire", "North Carolina", "California") PS C:\Windows\System32> Write-Host "States: $states" States: New Hampshire North Carolina California PS C:\Windows\System32> Write-Host "States:" $states States: New Hampshire North Carolina California
In the same way, even if you hade created the items each on its own line, they would still display on the same line with the content of Write-Host. Here are examples:
$customers = @("Thomas Stones" 'Ericka Dellaney' "Kelly Davids" "Marianne Harrington") Write-Host '==========================================================================' Write-Host "Customers: $customers" Write-Host '--------------------------------------------------------------------------' Write-Host "Customers:" $customers Write-Host '=========================================================================='
In the second lesson, we also saw that you can use Write-Output to display something on a computer screen. When it comes to Write-Output, if you write the name of the array outside the double-quotes of Write-Output, each member of the array would display on its own line. Consider the following code:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Kelly Davids", "Marianne Harrington") Write-Host '==========================================================================' Write-Host "Customers: $customers" Write-Host '--------------------------------------------------------------------------' Write-Host "Customers:" $customers Write-Host '==========================================================================' Write-Output "Customers: $customers" Write-Output '--------------------------------------------------------------------------' Write-Output "Customers:" $customers Write-Output '=========================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercise09\Exercise8.ps1 ========================================================================== Customers: Thomas Stones Ericka Dellaney Kelly Davids Marianne Harrington -------------------------------------------------------------------------- Customers: Thomas Stones Ericka Dellaney Kelly Davids Marianne Harrington ========================================================================== Customers: Thomas Stones Ericka Dellaney Kelly Davids Marianne Harrington -------------------------------------------------------------------------- Customers: Thomas Stones Ericka Dellaney Kelly Davids Marianne Harrington ==========================================================================
Accessing a Member of an Array
The most common way to use an array cosists of accessing an item from it, or one item at a time. To access a member of the array, type the name of the array variable with its $ symbol. This is followed by square brackets. In the square brackets, type the integer position of the member you want. The positions of the items in the array are 0-based. This means that the first item (the most left or the top) is at Position or Index 0. The second item is at Position 1 or Index 1. The third item is at Position 2 or Index 2, and so on. Here is an example that accesses the third item at Position 2 or Index 2:
$numbers = @(
12.44,
525.38,
6.28,
2448.32,
632.04
)
$numbers[2]
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 6.28
Of course, you can add other text where you are accessing an item from an array. Here are examples:
$numbers = @(
12.44,
525.38,
6.28,
2448.32,
632.04
)
Write-Host "Number 1: " $numbers[0]
Write-Host 'Number 2: ' $numbers[1]
Write-Host 'Number 3: ' $numbers[2]
Write-Host 'Number 4: ' $numbers[3]
Write-Host "Number 5: " $numbers[4]
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 Number 1: 12.44 Number 2: 525.38 Number 3: 6.28 Number 4: 2448.32 Number 5: 632.04
Primary Characteristics of an Array
A Fixed Number of Items
When you create an array, you indicate that you want a fixed number of items. This means that you cannot add a new item to an existing array (if you do, you would receive an error).
The Length of an Array
We saw that if you declare an array variable and specify its items, at any time, to know the number of items in an array, type the name of the array variable (including its $ symbol), followed by a period, followed by length or Length. Here is an example:
$numbers = @( 12.44, 525.38, 6.28, 2448.32, 632.04 ) Write-Output "=====================" Write-Host "Number 1: " $numbers[0] Write-Host 'Number 2: ' $numbers[1] Write-Host 'Number 3: ' $numbers[2] Write-Host 'Number 4: ' $numbers[3] Write-Host "Number 5: " $numbers[4] Write-Output "---------------------" Write-Host "Number of Items: " $numbers.Length Write-Output "====================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1
=====================
Number 1: 12.44
Number 2: 525.38
Number 3: 6.28
Number 4: 2448.32
Number 5: 632.04
---------------------
Number of Items: 5
=====================
Counting the Members of an Array
As another technique to get the number of items in an array, type the name of the array variable (with its $ symbol), followed by a period, and followed by count or Count. Here is an example:
$customers = "Thomas Stones", 'Ericka Dellaney', "Kelly Davids", 'Marianne Harrington', "Akhil Koumari", 'Mandiakandara Marmoudi' Write-Output "==================================" Write-Host "Customer 1:" $customers[0] Write-Host "Customer 2:" $customers[1] Write-Host "Customer 3:" $customers[2] Write-Host "Customer 4:" $customers[3] Write-Host "Customer 5:" $customers[4] Write-Host "Customer 6:" $customers[5] Write-Output "----------------------------------" Write-Host "Number of customers: " $customers.count
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ================================== Customer 1: Thomas Stones Customer 2: Ericka Dellaney Customer 3: Kelly Davids Customer 4: Marianne Harrington Customer 5: Akhil Koumari Customer 6: Mandiakandara Marmoudi ---------------------------------- Number of customers: 6
Primary Operations on an Array
Introduction
An array is a fixed list of items. When you create an array, you must specify, or kind of specify, its items. This is required because PowerShell wants to know the number of items in the array. As it happens, when you have created an array, there are operations you can perform on it and some operations are forbidden.
An Array of Null or Empty Items
Remember that when you create an array, you must at a minimum create a placeholder for each eventual item. If an item is a number, you can specify value as 0 or any number. Here is an example
$numbers = @(0, 0, 0, 0, 0)
If an item is a character, you can specify its value with two single quotes. If an item is a string, specify its value with two double-quotes. Here is an example
$names = @("", "", "", "", "")
When you have created an array like that, every item has its position in the array and PowerShell knows the number of items in the array, and that number cannot change.
Updating an Item
After creating an array, you are allowed to change the value of any item. To do that, type the name of the array variable followed by square brackets. In the square brackets, type the index of the item you want to access, then assign the desired value. Here are examples:
$customers = "Thomas Stones", 'Ericka Dellaney', "Kelly Davids", 'Marianne Harrington', "Akhil Koumari", 'Mandiakandara Marmoudi' Write-Output "==================================" Write-Output "Customers" Write-Output '----------------------------------' Write-Host "Customer 1:" $customers[0] Write-Host "Customer 2:" $customers[1] Write-Host "Customer 3:" $customers[2] Write-Host "Customer 4:" $customers[3] Write-Host "Customer 5:" $customers[4] Write-Host "Customer 6:" $customers[5] Write-Output "==================================" $customers[1] = "Jonathan Simmings" $customers[3] = "Miguel Altieri" $customers[5] = "Ronald Glassman" Write-Output "Customers" Write-Output '----------------------------------' Write-Host "Customer 1:" $customers[0] Write-Host "Customer 2:" $customers[1] Write-Host "Customer 3:" $customers[2] Write-Host "Customer 4:" $customers[3] Write-Host "Customer 5:" $customers[4] Write-Host "Customer 6:" $customers[5] Write-Output "=================================="
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ================================== Customers ---------------------------------- Customer 1: Thomas Stones Customer 2: Ericka Dellaney Customer 3: Kelly Davids Customer 4: Marianne Harrington Customer 5: Akhil Koumari Customer 6: Mandiakandara Marmoudi ================================== Customers ---------------------------------- Customer 1: Thomas Stones Customer 2: Jonathan Simmings Customer 3: Kelly Davids Customer 4: Miguel Altieri Customer 5: Akhil Koumari Customer 6: Ronald Glassman ==================================
An Empty Array
An empty array is an array that doesn't have any item. For any reason you judge necessary, you can create an empty array. To do that, type @(). It necessary, assign it to a variable. Here is an example:
$project = @()
Remember that an array has a fixed number of items, which means you cannot had a new item to an array. In the same way, if an array is empty, you cannot change or update its item, since it doesn't have any.
Sub-Arrays
Introduction
A sub array is an array created or derived from, or based on an existing array. This means that, to create a sub-array, you must first have created an array. This also means that the items in the new arrays can come only from another array. You have many options.
A Sub-Array from Designated Members of an Array
To access all members of an array one member at a time, we saw that you can use the index notation where you would put the index of the desired member in the square brackets applied to the name of the array. Here is one more example:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Kelly Davids", "Marianne Harrington" 'Angel Bulzaides', "Akhil Koumari", 'Mandiakandara Marmoudi', "Richard Eghert") Write-Output "================================" Write-Output "Customers" Write-Output "--------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Output "================================"
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ================================ Customers -------------------------------- Customer: Thomas Stones Customer: Ericka Dellaney Customer: Kelly Davids Customer: Marianne Harrington Customer: Angel Bulzaides Customer: Akhil Koumari Customer: Mandiakandara Marmoudi Customer: Richard Eghert ================================ Clients -------------------------------- Client: Ericka Dellaney Client: Angel Bulzaides Client: Mandiakandara Marmoudi ================================
Sometimes, you want only some specific members. To create a sub-array from an array, type the name of the array followed by square brackets. In the square brackets, type the index of each desired member. The indexes must be separated by comas. Remember that the indexes are 0based. If you are planning to use the sub-array many times, you can store it in a variable. Such a variable becomes its own array. You can access the items of that array using its own 0-based index. Here is an example:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Kelly Davids", "Marianne Harrington" 'Angel Bulzaides', "Akhil Koumari", 'Mandiakandara Marmoudi', "Richard Eghert") $clients = $customers[1, 4, 6] Write-Output "================================" Write-Output "Customers" Write-Output "--------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Output "================================" Write-Output "Clients" Write-Output "--------------------------------" Write-Host "Client:" $clients[0] Write-Host "Client:" $clients[1] Write-Host "Client:" $clients[2] Write-Output "================================"
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1
=====================
Number 1: 12.44
Number 2: 525.38
Number 3: 6.28
Number 4: 2448.32
Number 5: 632.04
---------------------
Number of Items: 5
=====================
If you are not planning to use the sub-array many times, you can accesss it directly where it is needed. To do that, type the name of the array variable followed by by square brackets. In the square brackets, type the coma-separated indexes of the desired members. Outside those brackets, add other square brackets. In the new square brackets, type the index of the desired member based on the sub-array. Here is an example:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Kelly Davids", "Marianne Harrington" 'Angel Bulzaides', "Akhil Koumari", 'Mandiakandara Marmoudi', "Richard Eghert") Write-Output "================================" Write-Output "Customers" Write-Output "--------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Output "================================" Write-Output "Clients" Write-Output "--------------------------------" Write-Host "Client:" $customers[0, 2, 5][0] Write-Host "Client:" $customers[0, 2, 5][1] Write-Host "Client:" $customers[0, 2, 5][2] Write-Output "================================"
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ================================ Customers -------------------------------- Customer: Thomas Stones Customer: Ericka Dellaney Customer: Kelly Davids Customer: Marianne Harrington Customer: Angel Bulzaides Customer: Akhil Koumari Customer: Mandiakandara Marmoudi Customer: Richard Eghert ================================ Clients -------------------------------- Client: Thomas Stones Client: Kelly Davids Client: Akhil Koumari ================================
A Sub-Array from a Range of Items
If you have an array, you can create another array by specify a range of items from a certain index to another index. To do that, type the name of the existing array variable followed by square brackets. In the square brackets, type an index for the lowest item you want to access, type .., followed by the index of the highest item you want to access. If you are planning to use the sub-array many times, you can store the sub-array in a variable. That variable becomes its own array with 0-based indexes. Here is an example:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Grace Brenner" "Kelly Davids", "Marianne Harrington", 'Angel Bulzaides' "Akhil Koumari", "Richard Eghert", 'Mandiakandara Marmoudi') $clients = $customers[3..6] Write-Output "=======================================" Write-Output "Customers" Write-Output "---------------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Host "Customer:" $customers[8] Write-Output "=======================================" Write-Output "Clients" Write-Output "---------------------------------------" Write-Host "Client: " $clients[0] Write-Host "Client: " $clients[1] Write-Host "Client: " $clients[2] Write-Host "Client: " $clients[3] Write-Output "======================================="
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ======================================= Customers --------------------------------------- Customer: Thomas Stones Customer: Ericka Dellaney Customer: Grace Brenner Customer: Kelly Davids Customer: Marianne Harrington Customer: Angel Bulzaides Customer: Akhil Koumari Customer: Richard Eghert Customer: Mandiakandara Marmoudi ======================================= Clients --------------------------------------- Client: Kelly Davids Client: Marianne Harrington Client: Angel Bulzaides Client: Akhil Koumari =======================================
If you are not planning to use the sub-array many times, you can just access it where it is needed. Here are examples:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Grace Brenner" "Kelly Davids", "Marianne Harrington", 'Angel Bulzaides' "Akhil Koumari", "Richard Eghert", 'Mandiakandara Marmoudi') Write-Output "=======================================" Write-Output "Customers" Write-Output "---------------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Host "Customer:" $customers[8] Write-Output "=======================================" Write-Output "Clients" Write-Output "---------------------------------------" Write-Host "Customers[2..5][0]:" $customers[2..5][0] Write-Host "Customers[2..5][1]:" $customers[2..5][1] Write-Host "Customers[2..5][2]:" $customers[2..5][2] Write-Output "---------------------------------------" Write-Host "Customers[0..3][0]:" $customers[0..3][0] Write-Host "Customers[0..3][1]:" $customers[0..3][1] Write-Host "Customers[0..3][2]:" $customers[0..3][2] Write-Output "---------------------------------------" Write-Host "Customers[1..4][0]:" $customers[1..4][0] Write-Host "Customers[1..4][1]:" $customers[1..4][1] Write-Host "Customers[1..4][2]:" $customers[1..4][2] Write-Output "======================================="
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ======================================= Customers --------------------------------------- Customer: Thomas Stones Customer: Ericka Dellaney Customer: Grace Brenner Customer: Kelly Davids Customer: Marianne Harrington Customer: Angel Bulzaides Customer: Akhil Koumari Customer: Richard Eghert Customer: Mandiakandara Marmoudi ======================================= Clients --------------------------------------- Customers[2..5][0]: Grace Brenner Customers[2..5][1]: Kelly Davids Customers[2..5][2]: Marianne Harrington --------------------------------------- Customers[0..3][0]: Thomas Stones Customers[0..3][1]: Ericka Dellaney Customers[0..3][2]: Grace Brenner --------------------------------------- Customers[1..4][0]: Ericka Dellaney Customers[1..4][1]: Grace Brenner Customers[1..4][2]: Kelly Davids =======================================
A Sub-Array from a Reverse Range of Items
In the previous technique, we considered a range of items from a low to a high value. You can also a range from a high to a lower index. In that case, the sub-array would include the items in the specified range, but the item from the high-selected index would be the lowest in the new sub-array. The item from the low-selected index of the original array would be the highest item in the new sub-array. The items in the range would have their positions reversed. Consider the following example:
$customers = @("Thomas Stones", 'Ericka Dellaney', "Grace Brenner" "Kelly Davids", "Marianne Harrington", 'Angel Bulzaides' "Akhil Koumari", "Richard Eghert", 'Mandiakandara Marmoudi') $clients = $customers[6..3] Write-Output "=======================================" Write-Output "Customers" Write-Output "---------------------------------------" Write-Host "Customer:" $customers[0] Write-Host 'Customer:' $customers[1] Write-Host 'Customer:' $customers[2] Write-Host 'Customer:' $customers[3] Write-Host "Customer:" $customers[4] Write-Host "Customer:" $customers[5] Write-Host "Customer:" $customers[6] Write-Host "Customer:" $customers[7] Write-Host "Customer:" $customers[8] Write-Output "=======================================" Write-Output "Clients" Write-Output "---------------------------------------" Write-Host "Client: " $clients[0] Write-Host "Client: " $clients[1] Write-Host "Client: " $clients[2] Write-Host "Client: " $clients[3] Write-Output "======================================="
This would produce:
PS C:\TaxPreparation05> . 'C:\Exercises\Arrays.ps1' ======================================= Customers --------------------------------------- Customer: Thomas Stones Customer: Ericka Dellaney Customer: Grace Brenner Customer: Kelly Davids Customer: Marianne Harrington Customer: Angel Bulzaides Customer: Akhil Koumari Customer: Richard Eghert Customer: Mandiakandara Marmoudi ======================================= Clients --------------------------------------- Client: Kelly Davids Client: Marianne Harrington Client: Angel Bulzaides Client: Akhil Koumari =======================================
Introduction
An array is primarily is a type in its own right. As such, it can be used in the various ways we have dealt with variables so far. The main issue to keep in mind is that the type of an array is a series of values.
An Array in a Function
In the bod of a function, you can declare an array variable and use it as you see fit. Here is an example:
function ShowStates { [string[]]$degrees = @("Associate", "Bachelor", "Master", 'PhD') Write-Output "=====================" Write-Host "Degree: " $degrees[0] Write-Host 'Degree: ' $degrees[1] Write-Host 'Degree: ' $degrees[2] Write-Host 'Degree: ' $degrees[3] Write-Output "---------------------" Write-Host "Number of Items: " $degrees.Length Write-Output "=====================" } ShowStates
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ===================== Degree: Associate Degree: Bachelor Degree: Master Degree: PhD --------------------- Number of Items: 4 =====================
Returning an Array from a Function
Like a normal variable, an array can be returned from a function. This means that the function would return a variable that carries various values. To proceed, in the body of the function, you can declare and initialize an array variable. Still in the body of the function, you can use the array any way you want, such as performing any operation or processing you want. Before the closing curly bracket, you must return a value that holds an array. As one way you can do this, in the body of the function, you can create and initialize an array variable, then return that variable. Here is an example:
function Initialize { # An array variable [int[]]$numbers = @(12, 5, 625, -2) # Returning the array variable return $numbers }
Once you have the returned array from a function, you can use that array normally. As one option, you can declare a normal array and assign the function call to it. Once you have done that, the new variable holds the array that the function returned. You can then use that variable as an array variable. Here is an example:
function Initialize { [int[]]$numbers = @(12, 5, 625, -2) return $numbers } $values = Initialize Write-Host "=====================" Write-Host "Number:" $values[0] Write-Host "Number:" $values[1] Write-Host "Number:" $values[2] Write-Host "Number:" $values[3] Write-Host "====================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ===================== Number: 12 Number: 5 Number: 625 Number: -2 =====================
If you know the array you want to return and you don't need to use it many times, you don't have to declare a variable for it. You can return it directly on the function. This can be done as follows:
function CreateVehicles {
return @("canoe", "boat", "ship", "submarine")
}
$water = CreateVehicles
Write-Host "====================="
Write-Host "Vehicle:" $water[0]
Write-Host "Vehicle:" $water[1]
Write-Host "Vehicle:" $water[2]
Write-Host "Vehicle:" $water[3]
Write-Host "====================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ===================== Vehicle: canoe Vehicle: boat Vehicle: ship Vehicle: submarine =====================
In the function, you can create a conditional statement that specifies the returned array based on a condition. Here is an example:
function GetPronouns($nbr) { $pronouns = @("", "", "", "", "", "") if($nbr -eq 1) { $pronouns = @("I", "you", "he/she", "we", "you", "they") } elseif($nbr -eq 2) { $pronouns = @("me", "you", "him/her", "us", "you", "them") } elseif($nbr -eq 3) { $pronouns = @("myself", "yourself", "himself/herself", "ourselves", "yourselves", "themselves") } else { $pronouns = @("unknown", "unknown", "unknown", "unknown", "unknown", "unknown") } return $pronouns } $calls = GetPronouns(3) Write-Host "=========================================================================" Write-Host "Pronouns:" $calls Write-Host "========================================================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ========================================================================= Pronouns: myself yourself himself/herself ourselves yourselves themselves =========================================================================
Once again, remember that you use a variable if you are planning to use a value many times. Otherwise, you may not need a variable. Here is an example:
function GetPronouns($nbr) { if($nbr -eq 1) { return @("I", "you", "he/she", "we", "you", "they") } elseif($nbr -eq 2) { return @("me", "you", "him/her", "us", "you", "them") } elseif($nbr -eq 3) { return @("myself", "yourself", "himself/herself", "ourselves", "yourselves", "themselves") } else { return @("unknown", "unknown", "unknown", "unknown", "unknown", "unknown") } } $calls = GetPronouns(1) Write-Host "==================================" Write-Host "Pronouns:" $calls Write-Host "=================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ================================== Pronouns: I you he/she we you they ==================================
Like a regular variable, an array can be passed as argument. To proceed, in the parentheses of a function you are creating, provide the name of the parameter. Here is an example:
function ShowPoints($points) {
}
When an array has been passed to a function, it can be used in the body of the function as any array would be, following the rules of array variables. For example, the simplest way you can use an array is to display the values of its members. This could be done as follows:
function DisplayPronouns($known) { Write-Host "=================" Write-Host "Pronoun:" $known[0] Write-Host "Pronoun:" $known[1] Write-Host "Pronoun:" $known[2] Write-Host "Pronoun:" $known[3] Write-Host "Pronoun:" $known[4] Write-Host "Pronoun:" $known[5] Write-Host "=================" } function GetPronouns($nbr) { if($nbr -eq 1) { return @("I", "you", "he/she", "we", "you", "they") } elseif($nbr -eq 2) { return @("me", "you", "him/her", "us", "you", "them") } elseif($nbr -eq 3) { return @("myself", "yourself", "himself/herself", "ourselves", "yourselves", "themselves") } else { return @("unknown", "unknown", "unknown", "unknown", "unknown", "unknown") } } $calls = GetPronouns(2) DisplayPronouns $callspts[0]
This would produce:
PS C:\Windows\System32> C:\Exercises\Arrays.ps1 ================= Pronoun: me Pronoun: you Pronoun: him/her Pronoun: us Pronoun: you Pronoun: them =================
|
|||
Previous | Copyright © 2001-2025, FunctionX | Wednesday 12 February 2025, 16:36 | Next |
|