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 Data Type of an Array
Consider the following array and how its members are used:
$employee = @(638046, "Manish", "Sanjeevkumar", 26.37, $TRUE) $fullName = $employee[1] + ' ' + $employee[2] $weeklySalary = $employee[3] * 40 $biweeklySalary = $employee[3] * 40 * 2 $monthlySalary = $employee[3] * 40 * 4 $yearlySalary = $employee[3] * 40 * 4 * 12 Write-Host '=========================================' Write-Host "Employee Record" Write-Host '=========================================' Write-Host "Employee #: " $employee[0] Write-Host "-----------------------------------------" Write-Host "Employee Name: $fullName" Write-Host "-----------------------------------------" if($employee[4] -eq $true) { Write-Output "Employment Status: Full-Time" } else { Write-Output "Employment Status: Part-Time" } Write-Host "-----------------------------------------" Write-Host "Hourly Salary: " $employee[3] Write-Host "-----------------------------------------" Write-Host ("Weekly Salary: {0:f2}" -f $weeklySalary) Write-Host "-----------------------------------------" Write-Host ("Biweekly Salary: {0:f2}" -f $biweeklySalary) Write-Host "-----------------------------------------" Write-Host ("Monthly Salary: {0:f2}" -f $monthlySalary) Write-Host "-----------------------------------------" Write-Host ("Yearly Salary: {0:f2}" -f $yearlySalary) Write-Host '========================================='
This would produce:
PS C:\Arrays> C:\Arrays\Exercise1.ps1 ========================================= Employee Record ========================================= Employee #: 638046 ----------------------------------------- Employee Name: Manish Sanjeevkumar ----------------------------------------- Employment Status: Full-Time ----------------------------------------- Hourly Salary: 26.37 ----------------------------------------- Weekly Salary: 1054.80 ----------------------------------------- Biweekly Salary: 2109.60 ----------------------------------------- Monthly Salary: 4219.20 ----------------------------------------- Yearly Salary: 50630.40 =========================================
Notice that the array contains different types of items. This means that PowerShell allows the items of an array to be of different types (many languages don't allow that). If you want, you can ask PowerShell to restrict all items of an array to be of the same type. To do that, you can specify the type of the array. To do that, if you are declaring a variable of an array, start the variable with square brackets: []. In the square brackets, enter the type of the array followed by square brackets. When you have done that, all of the members of the raay must be the type specifed. Here is an example:
[string[]]$employee = @('638046', "Manish", "Sanjeevkumar", '26.37', "True")
$fullName = $employee[1] + ' ' + $employee[2]
Write-Host '========================================='
Write-Host "Employee Record"
Write-Host '========================================='
Write-Host "Employee #: " $employee[0]
Write-Host "-----------------------------------------"
Write-Host "Employee Name: $fullName"
Write-Host "-----------------------------------------"
if($employee[4] -eq 'True')
{
Write-Output "Employment Status: Full-Time"
} else {
Write-Output "Employment Status: Part-Time"
}
Write-Host "-----------------------------------------"
Write-Host "Hourly Salary: " $employee[3]
Write-Host '========================================='
This would produce:
PS C:\Arrays> C:\Arrays\Exercise1.ps1 ========================================= Employee Record ========================================= Employee #: 638046 ----------------------------------------- Employee Name: Manish Sanjeevkumar ----------------------------------------- Employment Status: Full-Time ----------------------------------------- Hourly Salary: 26.37 =========================================
After specifying the data type of an array, if you provide an item that is not of the specified type, your program may produce an error or unpredictable results.
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.
|
|||
Previous | Copyright © 2001-2025, FunctionX | Monday 24 February 2025, 19:06 | Next |
|