Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of Linux arrays and associative arrays

Detailed explanation of Linux arrays and associative arrays

小云云
小云云Original
2018-03-31 13:48:032632browse

Array is a very important part of shell script. It stores multiple independent data as a collection with the help of index. Ordinary arrays can only use integers as array indexes. Bash also supports associative arrays, which can use strings as array indexes. In many cases, string indexing is easier to understand, and this is where associative arrays come in handy. Here, we will see the usage of ordinary arrays and associative arrays.

(1) There are many ways to define an array. You can define an array using a column of values ​​in a single row:
array_var=(1 2 3 4 5 6)
#These values ​​will be stored in consecutive positions starting from 0.
In addition, Arrays can also be defined as a set of "index-values":
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
array_var [3]="test4"
array_var[4]="test5"
array_var[5]="test6"

(2) Print out the contents of the array element at a specific index:

[rhx@localhost Test]$ vim arr.sh

[rhx@localhost Test]$ source arr.sh
[rhx@localhost Test]$ echo ${array[0]}
//输出:1
[rhx@localhost Test]$ echo ${array[*]}
//输出数组元素:1 2 3 4 5 6 7 8 9 10
[rhx@localhost Test]$ echo ${array[@]}
//输出数组元素1 2 3 4 5 6 7 8 9 10
[rhx@localhost Test]$ echo ${#array[@]}
//输出数组长度:10
[rhx@localhost Test]$ echo ${#array[*]}
//输出数组长度:10

Associative arrays were introduced starting with Bash version 4.0. With the help of hashing techniques, associative arrays become a powerful tool for solving many problems. Let’s find out next.
1. Define associative array

In an associative array, we can use any text as an array index. First, you need to use a declaration statement to declare a variable name as an associative array. Like the following:

[rhx@localhost Test]$declare -A ass_array

After the declaration, there are two ways to add elements to the associative array.
 Utilize the method of embedded "index-value" list to provide an "index-value" list:
$ ass_array=([index1]=val1 [index2]=val2)
 Use independent "Index-value" assignment:
$ ass_array[index1]=val1
$ ass_array'index2]=val2

For example, imagine how to use an associative array to set prices for fruits :

[rhx@localhost Test]$ declare -A fruit_value
[rhx@localhost Test]$ fruit_value=([apple]="100"[orange]="150")
[rhx@localhost Test]$ echo ${fruit_value[apple]}

2. List array index
Each array element has an index for search. Normal arrays and associative arrays have different index types. We can use the following method to get the index list of the array:
$ echo ${!array_var[*]}
You can also use:
$ echo ${!array_var[@]
precede Take the fruits_value array as an example. Run the following command:
$ echo ${!fruits_value[*]}
orange apple
This method is also feasible for ordinary arrays.

The above is the detailed content of Detailed explanation of Linux arrays and associative arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn