Shell script-Associative array

Source: Internet
Author: User

An associative array of shells

The

Shell associative Array
   bashi has no native support for similar hash table, unlike Perl or Python.
The subscript array element is accessed by an array subscript (an array subscript can be an arithmetic expression whose result must be an integer), but this access is limited when it expresses some data that is strongly correlated. The
Shell provides another array that can use any string as a subscript (not necessarily an integer) to access the array elements. This array is called an associative array (associative array). The subscripts and values of the
associative array are called key-value pairs, and they are one by one corresponding relationships. In an associative array, the key is unique and the value can be not unique. The associative array of the
shell and the associated array for Perl are functionally identical. In Perl, associative arrays are sometimes referred to as hashes (hash).

defines an associative array
Shell's associative array and the shell's subscript array are exactly the same on the definition and use, except that there are differences on the index. The
Note that you need to use the command declare-a array to display the declaration before using the associative array. The contents of the

Example 1
test.sh  file are as follows
Name= (Jim Tom Lucy)

declare-a phone
phone= ([jim]=135] [tom]=136 [ lucy]=158)

for I in ' eval echo {0..$ ((${#name [*]}-1)} '
do
    echo ${name[i]} phone Number is ${phone[' ${name[i]} '}
Done

Enter./test.sh at the command prompt, execution results are as follows:
Jim phone number is 135
Tom Phone Number is 136
Lucy phone number is 158

Action associative array syntax
The operation syntax of the associated array and the operation syntax for the arrays are exactly the same, and the common operations are listed below.

  Syntax   Description
 ${!array[*]}   fetch associative array all keys
 ${!array[@]}   take off Union array all keys
 ${array[*]}   fetch associative array all values
 ${array[@]   Take associative array all values
 ${#array [*]}
  length of associative array
  length of associative array


Example 2
The contents of the test.sh file are as follows
Declare-a Phone
Phone= ([jim]=135 [tom]=136 [lucy]=158]

For key in ${!phone[*]}
Do
echo "$key-${phone[$key]}"
Done

At the command prompt, enter the./test.sh and execute the following results:
Tom--136
Jim--135
Lucy--158


This article is from the "Linux Related Technologies" blog, make sure to keep this source http://wangergui.blog.51cto.com/8504247/1845353

Shell script-Associative array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.