- [Instructor] Arrays and associative arrays.
In the previous video, we've learned how to play with
file descriptors and redirection.
In this video, we'll see how to use both the arrays and
associative arrays.
Arrays are a very important component for storing a
collection of data as separate entities using indexes.
Regular arrays can use only integers as their array index.
On the other hand,
Bash also supports associative arrays.
They can take a string as their array index.
Associative arrays are very useful in many types
of manipulations where having a string index
makes more sense.
To use associative arrays,
you must have Bash version four, or higher.
An array can be defined in many ways.
Define an array using the list of values in a line as ...
Values will be stored in consecutive locations,
starting from ...
Ultimately, define an array as a set
of index value pairs, as ...
Print the contents of an array at a given index
using these commands.
Print all of the values in an array as a list
using these commands.
Alternatively, you could use ...
Print the length of an array, the number of elements
in an array, as ...
Associative arrays have been introduced to Bash
from version 4.
And they are useful entities to solve many problems
using the hashing technique.
Let us go into more detail.
Defining associative arrays.
In an associative array, we can use any text data
as an array index.
Initially, a declaration statement is required
to declare a variable name as an associative array.
This can be done as follows.
After the declaration, elements can be added to the
associative array using two methods, as...
While using in line index value list method,
we can provide a list of indexed value pairs.
Alternatively, you could use separate index value
assignments such as ...
For example, consider the assignment of price for fruits
using associative arrays.
Display the contents of an array as ...
Listing of array indexes.
Arrays have indexes for indexing each of the elements.
Ordinary and associative arrays
differ in terms of index time.
We can obtain the list of indexes in an array, as ...
Or we can also use ...
In the previous fruits_value array example,
consider this command.
This will work for ordinary arrays too.
Great!
In this video, we've successfully learned about arrays,
and associative arrays.
I hope you enjoyed watching this video.
In the next video, we'll learn about visiting aliases.