Linux Array & Associative Array

Ordinary Array:

Assign1: arrayName=(value1 value2 ...)

Assign2: arrayName[index]=value

Length: ${#arrayName[*]} or ${#arrayName[@]}

Indexes: ${!arrayName[*]} or ${#arrayName[@]}

Example:

#!/bin/bash

group1=(rio amos)
group2[0]=bill
group2[1]=frank
group2[2]=bruce

echo group1 has ${#group1[*]} person[s]
echo they are ${group1[*]}
echo group2 has ${#group2[@]} person[s]
echo they are ${group2[@]}

Associative Array:

Define: declare -A arrayName

Assign1: arrayName=([index1]=value1 [index2]=value2 ...)

Assign2: arrayName[index]=value

Length: ${#arrayName[*]} or ${#arrayName[@]}

Indexes: ${!arrayName[*]} or ${#arrayName[@]}

Example:

#!/bin/bash

declare -A team1
declare -A team2

team1=([rio]=1981 [amos]=1990)
team2[frank]=1983
team2[bill]=1984
team2[bruce]=1990

echo team1 has ${#team1[*]} person[s]
echo they are ${!team1[*]}

echo team2 has ${#team2[@]} person[s]
echo they are ${!team2[@]}

Linux Array & Associative Array

时间: 2024-10-08 04:41:10

Linux Array & Associative Array的相关文章

[C++] the pointer array & the array's pointer

void main(){ // the pointer array char* arry[] = { "hello", "world", "haha" }; for (int i = 0; i < 3; i++){ printf("string:%s,address:%p\n", arry[i], arry[i]); } // the array's pointer int a[10] = { 0, 1, 2, 3, 4

theme(&#39;item-list&#39;,array(&#39;items&#39;=&gt;array(&#39;aaa&#39;,&#39;bbb&#39;))) 是如何运行的 ?

$items['items'] = array( l('Configure', 'admin/config'), l('Structure', 'admin/structure'), ); $theme = theme('item_list', $items); 1,  判断是否所有的模块都加载了, if (!module_load_all(NULL) && !defined('MAINTENANCE_MODE')) { throw new Exception(t('theme() may

JavaScript定义数组的三种方式(new Array(),new Array(&#39;x&#39;,&#39;y&#39;),[&#39;x&#39;,&#39;y&#39;])

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

2018-6-3_《ES7的include、ES6的Array.of(),Array.from()及扩展符》

一. ES7的include //ES7,include-查找数组是否包含某个元素 返回布尔 let a=['OB','Koro1',1,NaN]; // let b=a.includes(NaN); // true 识别NaN // let b=a.includes('Koro1',100); // false 超过数组长度 不搜索 // let b=a.includes('Koro1',-3); // true 从倒数第三个元素开始搜索 // let b=a.includes('Koro1'

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

Array.of() &amp;&amp; Array()

Array.of(7) 创建一个具有单个元素 7 的数组, Array(7) 创建一个长度为7的空数组(注意:这是指一个有7个空位(empty)的数组,而不是由7个undefined组成的数组). Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3] Array.of(1); // [1] Array.of(1, 2, 3); //

Linux command&rsquo;s Array

#数组的声明与遍历 animals=("a dog" "a cat" "a fish") #wrong ways to use this for i in ${animals[*]}; do echo $i; done for i in ${animals[@]}; do echo $i; done for i in "${animals[*]}"; do echo $i; done #this is what we want

81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此时可能存在nums[start]=nums[end]或者nums[start]=n

154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. The array may contain duplicates. 思路:有重复元素的时候,不能按I中这么分三类(反例:Input: [3,3,1,3]=>将