takeaway

Hash tables are more complex data structures than arrays, called associative arrays or dictionaries in some languages, and so on. To put it simply, a hash table is used to store values corresponding to a given key. The relationship between keys and values is similar to the relationship between words and definitions in a dictionary. Definitions can be found quickly through words without having to iterate through matches from the beginning. To be precise, hash table is only one way to implement this function, but it can also be implemented using a variety of trees or other data structures. Different implementation methods are suitable for different scenarios, and the use method is the same. But to simplify the concept, we use the name hash table.

Hash table definition

Unlike other variable types, hash tables need to be declared in advance because the assignment syntax of hash tables is indistinguishable from that of arrays.

% typeset -A hashmap
# or use local, both functions are the same
% local -A hashmap

The syntax for assignment is the same as for arrays, but the order is key, value, key, value
% hashmap=(k1 v1 k2 v2)

Echo (); echo ()
% echo $hashmap
v1 v2

# use (kv) to output both keys and values, (kv) will place both keys and values in the same array
% echo ${(kv)hashmap}
k1 v1 k2 v2

The size of a hash table is the number of key-value pairs
% echo $#hashmap
2Copy the code

Elements, speaking, reading and writing

A hash table is read and written in much the same way as an array, except that the digits used for positioning are strings.

# can be declared and assigned to a line
% local -A hashmap=(k1 v1 k2 v2 k3 v3)
% echo $hashmap[k2]
v2

% hashmap[k2]="V2"

The method of deleting an element is different from that of an array
% unset "hashmap[k1]"
% echo ${(kv)hashmap}
k2 V2 k3 v3Copy the code

Hash table splicing

Append elements in the same way as arrays
% hashmap+=(k4 v4 k5 v5)
% echo $hashmap
V2 v3 v4 v5


% local -A hashmap1 hashmap2
% hashmap1=(k1 v1 k2 v2)
% hashmap2=(k2 v222 k3 v3)

Join hash table, expand to array and append
% hashmap1+=(${(kv)hashmap2})
If the key is duplicated, the value is replaced directly
% echo ${(kv)hashmap1}
k1 v1 k2 v222 k3 v3Copy the code

Hash table traversal

So, (kv), (k), and so on, convert the hash table into an array, and then iterate.

% local -A hashmap=(k1 v1 k2 v2 k3 v3)

Only the value is iterated
% for i ($hashmap) {>echo $i
> }
v1
v2
v3

Only traverse the key
% for i (${(k)hashmap}) {>echo $i
> }
k1
k2
k3

# Iterate over both keys and values
% for k v (${(kv)hashmap}) {>echo "$k -> $v"
> }
k1 -> v1
k2 -> v2
k3 -> v3Copy the code

Elements to find

Check whether the key exists.

% local -A hashmap=(k1 v1 k2 v2 k3 v3)
% (($+hashmap[k1])) && echo good
good
% (($+hashmap[k4])) && echo goodCopy the code

If you need to check whether a value exists, you can check the array of values. But that takes away the advantage of hashing tables.

% local -A hashmap=(k1 v1 k2 v2 k3 v3)
# value cannot also be a hash table, or can be forcibly declared as an array using local-a
% value=($hashmap)

% (( $value[(I)v1] )) && echo good
good
% (( $value[(I)v4] )) && echo goodCopy the code

Element ordering

The method of sorting hash table elements is similar to that of an array, with two more options k v, the rest of the options such as O (ascending), O (descending), N (by numeric size), I (ignore case), etc., not one by one example.

% local -A hashmap=(aa 33 cc 11 bb 22)

Sort by value only
% echo ${(o)hashmap}
11 22 33

# sort only on keys
% echo ${(ok)hashmap}
aa bb cc

Key values are sorted together
% echo ${(okv)hashmap}
11 22 33 aa bb ccCopy the code

Construct hash tables from strings, files

Since a hash table can be constructed from an array, constructing a hash table from a string or a file is the same as constructing an array.

% str="k1 v1 k2 v2 k3 v3"
% local -A hashmap=(${=str})
% echo $hashmap
v1 v2 v3Copy the code

Each element in the hash table is treated uniformly

The same is true for each element in the hash table, except that k and v are used to specify whether to handle the key or the value. No more examples.

% local -A hashmap=(k1 v1 k2 v2 k3 v3)
% print ${(U)hashmap}
V1 V2 V3

% print ${(Uk)hashmap}
K1 K2 K3

% print ${(Ukv)hashmap}
K1 V1 K2 V2 K3 V3Copy the code

:# can also be used on hash tables.

% local -A hashmap=(k1 v1 k2 v2 k3 v3)

# exclude matched values
% echo ${hashmap:#v1}
v2 v3

Print only matched keys
% echo ${(Mk)hashmap:#k[1-2]}
k1 k2Copy the code

conclusion

This article briefly covers the basic usage of hash tables. It’s not a long story, but because hash tables operate like arrays, many of the ways you manipulate arrays can be used on hash tables, and you can treat keys or values as separate arrays, manipulating hash tables is a little more complicated.

There are also more advanced ways of dealing with arrays and hash tables, which WE’ll talk about later.

Full article address: github.com/goreliu/zsh…

Pay to solve Windows, Linux, Shell, C, C++, AHK, Python, JavaScript, Lua and other fields related problems, flexible pricing, welcome to consult, wechat LY50247.