当前位置:网站首页>Awk from entry to earth (8) array
Awk from entry to earth (8) array
2022-07-04 08:46:00 【Wonderful binary】
Array
AWK There are data structures like associative arrays , The best feature of this data structure is that its index value does not need to be a continuous integer value . We can use both numbers and strings as the index of the array . besides , Associative arrays do not need to declare their size in advance , Because it can automatically increase or decrease at run time . This chapter will explain AWK How to use arrays .
The following is the syntax format used by the array :
array_name[index]=value
among array_name Is the name of the array ,index Is the array index ,value The value assigned to the element in the array .
Create array
To learn more about arrays , Let's first look at how to create an array and how to access array elements :
[jerry]$ awk 'BEGIN {
fruits["mango"]="yellow";
fruits["orange"]="orange"
print fruits["orange"] "\n" fruits["mango"]
}'
Execute the above command to get the following result :
orange
yellow
In the example above , We define a fruit (fruits) Array , The index of this array is the fruit name , Value is the color of the fruit . You can access array elements in the following format :
array_name[index]
Delete array elements
When inserting elements, we use assignment operators . When deleting array elements , We use delete sentence . As shown below :
delete array_name[index]
In the following example , Array orange The element is deleted ( The delete command has no output ):
[jerry]$ awk 'BEGIN {
fruits["mango"]="yellow";
fruits["orange"]="orange";
delete fruits["orange"];
print fruits["orange"]
}'
Multidimensional arrays
AWK Itself does not support multidimensional arrays , However, we can easily use one-dimensional array simulation to realize multi-dimensional array .
The following example is a 3x3 Three dimensional array of :
100 200 300
400 500 600
700 800 900
In the example above ,array[0][0] Storage 100,array[0][1] Storage 200 , By analogy . In order to be in array[0][0] Store 100, We can use the following syntax :
array["0,0"] = 100
Although in the example , We used 0,0 As index , But these are not two index values . in fact , It is a string index 0,0.
The following is an example of simulating a two-dimensional array :
[jerry]$ awk 'BEGIN {
array["0,0"] = 100;
array["0,1"] = 200;
array["0,2"] = 300;
array["1,0"] = 400;
array["1,1"] = 500;
array["1,2"] = 600;
# print array elements
print "array[0,0] = " array["0,0"];
print "array[0,1] = " array["0,1"];
print "array[0,2] = " array["0,2"];
print "array[1,0] = " array["1,0"];
print "array[1,1] = " array["1,1"];
print "array[1,2] = " array["1,2"];
}'
You can get the following result by executing the above command :
array[0,0] = 100
array[0,1] = 200
array[0,2] = 300
array[1,0] = 400
array[1,1] = 500
array[1,2] = 600
Many operations can be performed on arrays , such as , Use asort Finish sorting array elements , Or use asorti Implement the sorting of array index, etc . We will introduce functions that can operate on arrays in later chapters .
边栏推荐
- Mouse over to change the transparency of web page image
- From scratch, use Jenkins to build and publish pipeline pipeline project
- Basic discipline formula and unit conversion
- deno debugger
- Codeforces Global Round 21(A-E)
- HMS core helps baby bus show high-quality children's digital content to global developers
- 4 small ways to make your Tiktok video clearer
- How to set multiple selecteditems on a list box- c#
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- go-zero微服务实战系列(九、极致优化秒杀性能)
猜你喜欢

Codeforces Round #793 (Div. 2)(A-D)

Sequence model

Codeforces Round #803 (Div. 2)(A-D)

Codeforces Round #793 (Div. 2)(A-D)

The basic syntax of mermaid in typera

Developers really review CSDN question and answer function, and there are many improvements~

awk从入门到入土(12)awk也可以写脚本,替代shell
![[CV] Wu Enda machine learning course notes | Chapter 9](/img/de/41244904c8853b8bb694e05f430156.jpg)
[CV] Wu Enda machine learning course notes | Chapter 9

How to re enable local connection when the network of laptop is disabled

SSRF vulnerability exploitation - attack redis
随机推荐
User login function: simple but difficult
awk从入门到入土(15)awk执行外部命令
Sequence model
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
es6总结
FOC control
FRP intranet penetration, reverse proxy
The upper layer route cannot Ping the lower layer route
AcWing 244. Enigmatic cow (tree array + binary search)
Live in a dream, only do things you don't say
Comprendre la méthode de détection des valeurs aberrantes des données
Turn: excellent managers focus not on mistakes, but on advantages
Démarrage des microservices: passerelle
Four essential material websites for we media people to help you easily create popular models
OpenFeign 服务接口调用
How to get bytes containing null terminators from a string- c#
Basic discipline formula and unit conversion
System disk expansion in virtual machine
Ehrlich sieve + Euler sieve + interval sieve