课时56:数组实践_综合实践_数组关联

1.2.1 数组关联

学习目标

这一节,我们从 基础知识、简单实践、小结 三个方面来学习。

基础知识

简介

powershell 复制代码
	上一节,我们学习了shell环境下的数组定制的简写方式。数组的定制主要有如下两种:
定制索引数组 - 数组的索引是普通的数字
	declare -a array_name
	- 普通数组可以不事先声明,直接使用
	
定制关联数组 - 数组的索引是自定义的字母
	declare -A array_name
	- 关联数组必须先声明,再使用

简单实践

实践1-定制索引数组

powershell 复制代码
定制一个空内容的数组
[root@localhost ~]# declare -a course
[root@localhost ~]# declare -a | grep course
declare -a course='()'

定制一个包含元素的数组
[root@localhost ~]# course=(yuwen shuxue yingyu)
[root@localhost ~]# declare -a | grep course
declare -a course='([0]="yuwen" [1]="shuxue" [2]="yingyu")'

实践2-定制关联数组

powershell 复制代码
定制关联数组
[root@localhost ~]# declare -A score
[root@localhost ~]# declare -a | grep score
[root@localhost ~]# declare -A | grep score
declare -A score='()'

关联数组和数字索引数组不能通用
[root@localhost ~]# declare -a score
-bash: declare: score: 无法将关联数组转化为索引数组
powershell 复制代码
关联数组的操作
[root@localhost ~]# declare -A | grep score
declare -A score='([yingyu]="32" [yuwen]="67" [shuxue]="65" )'
[root@localhost ~]# echo ${!score[@]}
yingyu yuwen shuxue
[root@localhost ~]# echo ${score[@]}
32 67 65

小结

复制代码
相关推荐
秋已杰爱几秒前
进程间关系与进程守护
运维·服务器
微尘8几秒前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
可儿·四系桜14 分钟前
如何在多台Linux虚拟机上安装和配置Zookeeper集群
linux·服务器·zookeeper
Flying_Fish_roe17 分钟前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian
大广-全栈开发1 小时前
centos 7 安装gitlab
linux·git·centos
666786661 小时前
Mysql高级篇(中)—— SQL优化
linux·运维·服务器·数据库·sql·mysql
pemper_1 小时前
怎么操作使http变成https访问?
网络·网络协议·http·https·ssl
蒙奇·D·路飞-1 小时前
古诗词四首鉴赏
网络
宇宙第一小趴菜2 小时前
虚拟机安装xubuntu
linux·服务器·vmware
悲伤的创可贴2 小时前
Docker安装以及简单使用
linux·docker·centos