当前位置:网站首页>8、 Definition of array
8、 Definition of array
2022-07-27 01:32:00 【JXin-xxx】
8、 ... and 、 Definition of array
An array is a collection of data of the same type , It opens up a continuous space in memory , Usually used together with recycling
The classification of arrays
Normal array : Direct definition without declaration , Subscript index can only be an integer
Associative array : Need to use declare -A Otherwise, the system does not recognize it , The index can be a string
How to define an array
| ( | 11 | 22 | 33 | 44 | 55 | 66 | ) |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 |
The first one is : Directly enclose the elements to be added to the array in parentheses , Separate... With spaces in the middle
num=(11 22 33 44)
${#num} Shows the length of the string
Array name =(value0 value1 value2)
The second kind : Precisely define a value for each subscript index and add it to the array , Index numbers can be discontinuous
num=([0]=55 [1]=66 [2]=77 [4]=88)
Array name =([0]=value [1] =value [2] =value...)
The third kind of : First assign all the elements to be added to the array to a variable , Then apply this variable to the array
list=“11 12 13 14” List name =“value0 value1 value2...”
num=($list) Array name =($ List name )
A fourth : Define according to the subscript
Array name [0]="11" Array name [0]="value"
Array name [1]="22" Array name [1]="value"
Array name [2]="33" Array name [2]="value"
The data types included in the array
value type
Character type : Use “ ” or ‘ ’ Definition
Gets the length of the array
arr_number=(10 20 30 40 50)
arr_length=${#arr_number[*]}
or : arr_length=${#arr_number[@]}
echo $arr_length
echo ${arr_number[*]}
or echo ${arr_number[@]}
Array element traversal
[[email protected] ~]# vim ysszbl.sh
[[email protected] ~]# sh ysszbl.sh
1
2
3
4
5
6
[[email protected] ~]# cat ysszbl.sh
#!/bin/bash
arr=(1 2 3 4 5 6)
for i in ${arr[*]} # for i in ${arr[@]}
do
echo $i
done
Element slice
[[email protected] ~]# arr=(1 2 3 4 5 6 7 8)
[[email protected] ~]# echo ${arr[@]} # Output the entire array
1 2 3 4 5 6 7 8
[[email protected] ~]# echo ${arr[*]:2:3} # Extract subscript from index 2 At the beginning 3 Elements
3 4 5
[[email protected] ~]# echo ${arr[*]:0:4} # Extract subscript 0 At the beginning 4 Elements
1 2 3 4
[[email protected] ~]#
Array ( Elements ) Replace
[[email protected] ~]# arr=(11 12 13 14 15 16)
[[email protected] ~]# echo ${arr[*]/3/44} #${ Array name 【@ or *】/ To find the character / Replace character }
11 12 144 14 15 16
[[email protected] ~]# echo ${arr[*]/1/32}
321 322 323 324 325 326
[[email protected] ~]# echo ${arr[*]} # It will not replace the original contents of the array
11 12 13 14 15 16
[[email protected] ~]# arr=(${arr[*]/1/32}) # To change the original array , This can be achieved by reassigning
[[email protected] ~]# echo ${arr[*]}
321 322 323 324 325 326
Array delete
[[email protected] ~]# arr=(1 2 3 4 5)
[[email protected] ~]# unset arr # Delete array
[[email protected] ~]# echo ${arr[*]}
[[email protected] ~]# arr=(1 2 3 4 5)
[[email protected] ~]# unset arr[4] # Delete the subscript as 4 The elements of
[[email protected] ~]# echo ${arr[*]}
1 2 3 4
[[email protected] ~]#
Summary : Array
1、 Array definition
num=(11 12 13 14 11 15)
The above means to define a set ( Define all elements of an array )
You can use the following command to view the value of each element of the array and the length of the array
2、 Viewing the length of an array
echo ${num[@]}
echo ${num[*]}
3、 Viewing the length of an array
echo ${#num[@]}
echo ${#num[*]}
Check out the elements (echo) ${ Array name [ Subscript ]}
"@“ or ”*" Express all
4、 Element assignment
arr[0]=“12”
Array name [ Subscript ]= Element value
5、 Array slice
echo ${arr[*]:2:1}
echo ${ Array name [*]: Subscript : Print several element values }
echo ${ Array name [@]: Subscript : Print several element values }
6、 Array ( Elements ) Replace ----- temporary
echo ${arr[*]/3/55}
echo ${ Array name [*]/ character ( strand )/ Replace with the specified value ( Replace only the first match of each element )}
echo ${ Array name [@]/ character ( strand )/ Replace with the specified value ( Replace only the first match of each element )}
7、 Array delete
10 20 30 40 50
0 1 2 3 4
unset arr[2] # Delete the subscript as 2 The elements of ------------------》
10 20 40 50
0 1 2 3 4
Random roll call in the form of an array
[[email protected] opt]# cat dm.sh
#!/bin/bash
list=$(cat /opt/name) #/opt/name Roll call list
arr=($list) # Define an array
for i in {arr[*]}
do
a=$[RANDOM%25] #a Is the subscript value ,0-24 random number
echo "${arr[$a]}"
done
[[email protected] opt]#
边栏推荐
- Unity screenshot widget
- 【unity】Unity界面scene视图[1]
- Unity CharacterController
- Jenkins -- Basic -- 5.1 -- system configuration -- plug-in management
- 5. Legal bracket string
- Li Kou brushes 300 record posts
- Next generation Internet: Video Networking
- ESP8266连接乐鑫云平台IOT_Demo
- matlab基本介绍[1]
- As 5g becomes more and more popular, what positive effects will our lives be affected
猜你喜欢
随机推荐
MakeFile
集中式版本控制工具代码合并问题
软件测试面试题之xpath
What is the digital economy and how does it change the business model?
Pit encountered by AssetBundle
Unity Twitter登录接入
hdc_std
Mqtt protocol ----- above
七、循环语句
ESP8266 AP_ TCP_ Server
Come and help you understand the mobile Internet in a minute
ESP8266 AP_UDP_Server
ESP8266 STA_UDP_Client
SQL relational algebra - Division
Come on: encourage college graduates to return home to start businesses and employment, and help rural revitalization
C语言之数据存储汇总
How does KS catch salt value? api,did?
Software Foundation of software test interview questions
Database interim (II)
Unity[1] learning directory








