Topics on Arrays
Topics on Arrays
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 { param($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. As one way to proceed, when creating the function, add some parentheses to it. In the parentheses of the function, provide the name of the parameter. Here is an example:
function ShowPoints($points)
{
}
As we know already, another way to have a parameter is to create a param() section in parentheses-ess function. In the parentheses of param(), provide the name of the parameter. Here is an example:
function ShowPoints
{
param($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 =================
The Data Type of an Array Parameter
As done with array variables, you can specify the data type of a parameter created as an array. To do that, start the parameter with square brackets. In those square brackets, enter the type of the array followed by square brackets. Here is an example:
function ShowEmployee
{
param([string[]]$info)
$fullName = $info[1] + ' ' + $info[2]
Write-Host "Employee Record"
Write-Host '========================================='
Write-Host "Employee #: " $info[0]
Write-Host "-----------------------------------------"
Write-Host "Employee Name: $fullName"
Write-Host "-----------------------------------------"
if($info[4] -eq 'True')
{
Write-Output "Employment Status: Full-Time"
}
else
{
Write-Output "Employment Status: Part-Time"
}
Write-Host "-----------------------------------------"
Write-Host "Hourly Salary: " $info[3]
}
[string[]]$staff = @("283749", 'Sonia', "Grandfield", '22.837', "False")
Write-Host '========================================='
ShowEmployee -info $staff
Write-Host '========================================='
This would produce:
PS C:\Arrays> C:\Arrays\Exercise1.ps1 ========================================= Employee Record ========================================= Employee #: 283749 ----------------------------------------- Employee Name: Sonia Grandfield ----------------------------------------- Employment Status: Part-Time ----------------------------------------- Hourly Salary: 22.837 =========================================
|
|||
Previous | Copyright © 2001-2025, FunctionX | Monday 24 February 2025, 19:14 | Next |
|