The document provides an overview of arrays, highlighting their definition as homogeneous data structures that can be ordered and processed. It discusses array declarations, initializations, index notation, and the differences between indexed and associative arrays in programming languages like Bash and PowerShell. Additionally, it covers the concept of associative arrays and their implementations, as well as resources for further learning.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views20 pages
05 01 Arrays
The document provides an overview of arrays, highlighting their definition as homogeneous data structures that can be ordered and processed. It discusses array declarations, initializations, index notation, and the differences between indexed and associative arrays in programming languages like Bash and PowerShell. Additionally, it covers the concept of associative arrays and their implementations, as well as resources for further learning.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20
Arrays
(Homogeneous Data Types)
Mitra @ University of Texas at San Antonio 0
What Is an Array? • So far, you’ve have worked with variables one by one. • In the real world, we combine single elements to create larger entities with some meaning. • In computer languages, lists are often implemented as arrays. • One way to organize items is in an ordered array or list of items, such that the list can process data (sorting, searching, filtering, etc.) • Arrays and lists are homogeneous data structures of “like things”, or similar types of elements, that can be ordered and processed. Mitra @ University of Texas at San Antonio 1 What Is an Array? (cont’d.) • Arrays and lists are homogeneous data structures of “like things”, or similar types of elements, that can be ordered and processed. • One important exception! ◦ Although most programming languages require arrays to hold the same data type in each “element”, PowerShell does not require this. ◦ However, it is considered best practice to strongly type an array to restrict it to one type.
Mitra @ University of Texas at San Antonio 2
The Array in Computer Memory • The array type is typically illustrated with a series of contiguous memory locations. • We access the elements, or actual values, of an array with their index, in other words, their location. Index 0 1 2 3 4 5 6 7 8 9
Value 78 11 55 98 45 16 86 14 34 78
• The starting index often starts at 0 (rather than 1).
• The values can be of any data type, primitive or user- defined.
Mitra @ University of Texas at San Antonio 3
The Array in Computer Memory (cont’d.) • An array variable is identified by a name. Let’s call this array of strings “spices”. Index Value 0 cardamon 1 cinnamon 2 cloves 3 coriander 4 fennel 5 ginger 6 nutmeg 7 paprika 8 saffron 9 turmeric
Mitra @ University of Texas at San Antonio 4
Array Declarations • The syntax below creates the spices array with five elements in Bash and Powershell: ◦ Bash: declare –a spices=(5) ◦ PowerShell: $spices=[Array]::CreateInstance([Object],5) • What are some of the similarities and differences between these examples?
Mitra @ University of Texas at San Antonio 5
Array Initializations • Arrays can be declared and initialized at the same time. ◦ In Bash: ◦ declare –a spices=(‘cardamon’ ‘paprika’ ‘ginger’ ‘saffron’ ‘turmeric’ ) ◦ In PowerShell: $spices = @(‘cardamon’,‘paprika’,‘ginger’,‘saffron’,‘turmeric’) $spices = ‘cardamon’,‘paprika’,‘ginger’,‘saffron’,‘turmeric’ • What are the similarities and differences between these three array declarations?
Mitra @ University of Texas at San Antonio 6
Index Notation • Index notation is used to access the elements of an array. • Most programming languages use square brackets [] as the array index operator. • We can also assign values to array elements in a program: ◦ Bash: spices[3]=‘cumin’ ◦ PowerShell: $spices[3] = ‘cumin’
Mitra @ University of Texas at San Antonio 7
Processing All Array Members • We can visit all array elements using a for loop or built-in methods with special characters. Below are some examples. ◦ Bash: for index in “${spices[@]}” do echo “$index”; done ◦ PowerShell: simply type in $spices or foreach ( $index in $spices ) { "[$index]" } or (with a descriptor)
$spices | ForEach-Object {“spice: [$PSItem]"}
Mitra @ University of Texas at San Antonio 8 Key Aspects of Arrays • Arrays in semantically similar in programming languages (elements, index, length). However, they differ in implementation and syntax. For example: ◦ Bash arrays are either indexed (numeric) or associative (strings). ◦ PowerShell offers many ways to iterate across its array. • Two key functions of arrays: ◦ Sorting: places elements of a list in a certain order. ◦ Searching: find an element based on an index
Mitra @ University of Texas at San Antonio 9
Associative Arrays • Some data types use a hash, or key, rather than an index to access their associated value(s). ◦ A hash is a string that can directly access any element, whether sorted or not. ◦ You will run into the key-value pair concept in many other contexts in programming and scripting. ◦ Hashes are a key security feature in encrypted technologies (e.g., Blockchain). • These data types are referred to as associative arrays, maps, dictionaries, among others. • The “value” may be a primitive type, a record, a class, or some other entity. Mitra @ University of Texas at San Antonio 10 Implementations • Associative lists and maps can be implemented in several ways, including: ◦ Associative arrays (Bash) ◦ Hash tables (PowerShell) ◦ Dictionaries (Python) ◦ Maps (Java) • Associative arrays, dictionaries, and maps all use hashes/keys to access elements/values.
Mitra @ University of Texas at San Antonio 11
Bash and PowerShell Associative Arrays • We can declare associative arrays in both Bash and PowerShell. ◦ Bash declare A spices=( [number]=1 [spice]=turmeric [color]=yellow ) ◦ PowerShell $spices = @{} $spices = @{ number = 1; spices = “turmeric"; color = “yellow"}
Mitra @ University of Texas at San Antonio 12
PowerShell Hashtables • This video provides an overview of HashTables in PowerShell (Borsen, 2021) [25:18].
Mitra @ University of Texas at San Antonio 13
Bash Arrays • This video describes Bash arrays (Bash Tutorial 6, quidsup, 2016) [6:52]. We will work with the functions and scripts mentioned in Module 6/Lab 4.
Mitra @ University of Texas at San Antonio 14
Try It! • Bash arrays are simpler and more complex at the same time than in standard programming languages such as Python/Java. • Although Bash arrays may not seem as efficient or intuitive to use as in other languages, it’s useful to make comparisons since this course works mostly with Bash. • Read through this overview from Linux Magazine (Fioretti, Linux Magazine, 2019), and try out a few examples.
Mitra @ University of Texas at San Antonio 15
Summary • This lesson cover common aspects of arrays, such as: ◦ Indexed elements in brackets ◦ Fixed length ◦ Built-in libraries of functions • We also looked at how arrays can be implemented in Bash and PowerShell to provide you with a comparison. • You will become more familiar with the functionality and syntax of arrays in a particular language as you work with it.
Mitra @ University of Texas at San Antonio 16
Resources on Associative Arrays • Associative array in Bash (Yesmin, 2019) • Take control of your data with associative arrays in Bash (Kenlon, 2020) • about_Hash_Tables (Microsoft, 2021) • Top 51 PowerShell Examples You Should Learn (SPGuides, 2018)
Mitra @ University of Texas at San Antonio 17
Optional: History of Associative Arrays • Watch Brian Kernighan, the historic co-developer of Unix and the co-author of the first book on C, explain associative arrays (or hashmaps) (Essentials: Brian Kernighan on Associative Arrays – Computerphile, 2017) [10:33]
Mitra @ University of Texas at San Antonio 18
Optional: The Concept of Dictionaries • The PowerShell HashTable is a type of dictionary, as noted in the Array slide deck. Dictionaries contain records of pair mappings with a key and value and are a feature of many languages. • Watch a high-level explanation of the dictionary data structure.